简体   繁体   English

如何在 angularJs 中访问 stdclass json 对象?

[英]How to access stdclass json object in angularJs?

My JSON is in the form of stdclass object.我的 JSON 是 stdclass 对象的形式。 How can I iterate through my JSON.如何遍历我的 JSON。 My JSON is like:我的 JSON 是这样的:

$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.提前致谢。

this look like an php object.这看起来像一个 php 对象。 you should encode it in php before sending it to your JS在将其发送到您的 JS 之前,您应该在 php 中对其进行编码

  // PHP side:
  return json_encode(yourObject)

then return it to the JS and parse it然后返回给JS并解析

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

You should get a JSON like this :你应该得到这样的 JSON:

$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] .如果您可以访问数组的第一个元素: $scope.mytest[0]

if you want to access to a property of an object : yourObject["keyname"] example :如果要访问对象的属性: yourObject["keyname"]示例:

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"
            }
        }
    ]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM