keerthi keerthi - 6 months ago 28
JSON Question

How to access stdclass json object in angularJs?

My JSON is in the form of stdclass object. How can I iterate through my JSON. My JSON is like:

$scope.mytest=[ array(161) {
[0]=>
object(stdClass)#4 (14) {
["arrivalTime"]=>
string(8) "17:00:00"
["availableSeats"]=>
string(2) "42"
["boardingPointDetails"]=>
array(6) {
[0]=>
object(stdClass)#5 (3) {
["code"]=>
string(4) "1631"
["name"]=>
string(9) "Koyambedu"
["time"]=>
string(8) "09:30:00"
}
....
}];


Please help me. Thanks in advance.

Answer

this look like an php object. you should encode it in php before sending it to your JS

  // PHP side:
  return json_encode(yourObject)

then return it to the JS and parse it

// JS side
$http.get(...).then(function(res){
      JSON.parse(res) 
      ...
});

You should get a JSON like this :

$scope.mytest = [  { 
        "0" : {
            "arrivalTime" : "17:00:00",
            "availableSeats" : "42",
            "boardingPointDetails" : [{
                    "0" : {
                        "code" : "1631",
                        "name" : "Koyambedu",
                        "time" : "09:30:00"
                    }
                }
            ]
        }
        ....
    }
];

if you can access to the first element of an array : $scope.mytest[0].

if you want to access to a property of an object : yourObject["keyname"] example :

var myobject = {
    "arrivalTime" : "17:00:00",   // myobject["arrivalTime"] or myobject.arrivalTime
    "availableSeats" : "42",
    "boardingPointDetails" : [   // myobject.boardingPointDetails
         {                   // myobject.boardingPointDetails[0]
            "0" : {               // myobject.boardingPointDetails[0]["0"]
                "code" : "1631",  // myobject.boardingPointDetails[0]["0"]["code"]
                "name" : "Koyambedu",
                "time" : "09:30:00"
            }
        }
    ]
}
Comments