Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Value array json: How to get elements with Angular js?

Tags:

json

angularjs

The call on the api returns this json:

[
{
    "RESULT": {
        "TYPES": [
            "bigint",
            "varchar",
            "varchar",
            "varchar",
            "varchar",
            "varchar",
            "date",
            "varchar",
            "int",
            "int",
            "varchar"
        ],
        "HEADER": [
            "kvk",
            "bedrijfsnaam",
            "adres",
            "postcode",
            "plaats",
            "type",
            "anbi",
            "status",
            "kvks",
            "sub",
            "website"
        ],
        "ROWS": [
            [
                "273121520000",
                "Kinkrsoftware", <-- this is the value i want
                "Oude Trambaan 7",
                "2265CA",
                "Leidschendam",
                "Hoofdvestiging",
                null,
                null,
                "27312152",
                "0",
                null
            ]
        ]
    }
}
]

I can't change the api code.

I am using Angular and I can't see to get access to the values.

This is my controller:

  .controller('MainCtrl', function($scope, $http, $log, kvkInfo) {

  kvkInfo.success(function(status, data) { 

        $scope.name = status;
    $scope.bedrijf = data;
    $scope.status = status;
      });


});

I have tried

data.RESULT.ROW, data.RESULT.ROW[1], data.RESULT[0].ROW, data.RESULT[0].ROW[1], data.ROW[1]

How can i get this element?

like image 361
Ruudje Avatar asked Nov 30 '13 17:11

Ruudje


2 Answers

What you get starts with [, so it's an array. So you need data[0].

The first element of this array (data[0]) is an object (it starts with {) which has a RESULT attribute. So you can use data[0].RESULT.

The value of the RESULT attribute is another object which has a ROWS attribute (Note the final S). So you can use data[0].RESULT.ROWS.

The value of ROWS is an array, containing another array, so you need data[0].RESULT.ROWS[0][1].

like image 54
JB Nizet Avatar answered Nov 03 '22 11:11

JB Nizet


Your api result is wrapped in an array, so you have to save the first element of it to the scope instead of the whole array.

like image 24
peaceman Avatar answered Nov 03 '22 12:11

peaceman