0

I am trying to acces an array in array in my json object. This is my code to get data:

   function activate() {

        $http({
            method: 'GET',
            url: 'Dashboard/GetDashboard'
        }).then(function successCallback(response) {
            var id = $scope.dashboard = response.data;
            var datasource = id.Widgets[1].datasource;
            console.log(datasource);
           // var widgets = tryJsonConvert($scope.dashboard.Widgets);
            $http({
                url: 'Dashboard/GetDataSource/', params: { datasource: datasource },
                method: 'GET'
            }).then(function succesCallback(response) {
                $scope.datasource = response.data.datasource;
            });

        }, function errorCallback(response) {
            console.log(response);
        });
    }

The url: 'Dashboard/GetDashboard' gives me the following json object back:

 {  
   "Id":1,
   "UserId":2336276,
   "Name":"Data",
   "Widgets":[  
      {  
         "Id":0,
         "description":"Test1",
         "datasource":"Kri",
         "charttype":"Bar",
         "x":0,
         "y":0,
         "width":3,
         "height":2
      },
      {  
         "Id":0,
         "description":"Test2",
         "datasource":"Kpi",
         "charttype":"Heatmap",
         "x":3,
         "y":0,
         "width":3,
         "height":2
      },
      {  
         "Id":0,
         "description":"Test3",
         "datasource":"Loss events",
         "charttype":"Pie",
         "x":6,
         "y":0,
         "width":3,
         "height":2
      },
      {  
         "Id":0,
         "description":"Test4",
         "datasource":"Loss events",
         "charttype":"Area",
         "x":9,
         "y":0,
         "width":3,
         "height":2
      }
   ]
}

Now I want to acces datasource from widgets with the second GET operator and get the following error:

Error

And this is my controller method in C#:

[HttpGet]
    public string GetDataSource(int id)
    {
        var type = (DataSourceType)id;
        var o = _dashboarBusiness.GetDataSource(type = DataSourceType.KRI);
        return Newtonsoft.Json.JsonConvert.SerializeObject(o);
    }

I already look at this stack overflow post but it didn't work for me.

How to access an array in a JSON object?

I want to bind the var datasource on the param in the URL in the second GET operator.

What am I doing wrong?

Kind regards

UPDATE enum class:

public enum DataSourceType
    {
        CONTROL = 1,
        RISK = 2,
        MOI = 3,
        LOSSEVENTS = 4,
        KRI = 5
    }
7
  • 1
    Change the parameter id in controller to string first. Since you are sending string on second, but controller takes integer argument, so you are getting error. Commented Jan 9, 2018 at 10:40
  • Hi Subash thank you for you time. When I changed it to a string I get a error on var type = (DataSourceType)id; because I can't cast DataSourceType to a string. Commented Jan 9, 2018 at 10:45
  • DataSourceType is an enum right? Commented Jan 9, 2018 at 10:46
  • 1
    Remove var type = (DataSourceType)id; with DataSourceType type ; Enum.TryParse(id, out type ); Remember it wont work if you are using spaces in id value. Commented Jan 9, 2018 at 10:53
  • 1
    mention not bro, just a upvote. lol Commented Jan 9, 2018 at 11:38

1 Answer 1

0

made follwoing changes

  1. made int to string

    [HttpGet] public string GetDataSource(string id)

  2. params: { datasource: datasource } to params: { id: datasource }

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.