在javascript中迭代json

时间:2013-02-18 14:07:45

标签: arrays json

想要遍历此路线 - > bounds-> leg->步骤和警报(路径)。我是新手使用json,请你告诉我如何实现这一点。提前谢谢。

JSON是 -

    {"routes":[{"bounds":{"copyrights":"Map data ©2013 Google"},
"legs":["steps":[{"distance":{"text":"1 ft","value":0},
"path":[{"ib":39.049870000000006,"jb":-76.51013}],
"lat_lngs":[{"ib":39.049870000000006,"jb":-76.51013}],
"start_point":{"ib":39.04987000000001,"jb":-76.51013},
"end_point":{"ib":39.04987000000001,"jb":-76.51013}}],
"via_waypoint":[],"via_waypoints":[]}],"status":"OK"}

1 个答案:

答案 0 :(得分:0)

第一步是解析您收到的JSON字符串。 然后,您可以通过点表示法访问所有属性:

使用jQuery你可以这样做:

//{
//  "Name": "Apple",
//  "Expiry": "2008-12-28T00:00:00",
//  "Price": 3.99,
//  "Sizes": [
//    "Small",
//    "Medium",
//    "Large"
//  ]
//}


var json = '{"Name": "Apple","Expiry": "2008-12-28T00:00:00","Price": 3.99,"Sizes": ["Small","Medium","Large"]}';

var obj = jQuery.parseJSON(json);


$.each(obj.Sizes, function(index, item) {
   $("body").append("<p>" + item + "</p");
}); 

看看我如何在解析的JSON字符串上访问Sizes属性: obj.Sizes