[英]How can I get the object in JSON using jquery or javascript
I am new to JSON 我是JSON新手
How can I display the following data to the html. 如何将以下数据显示到html。 for sample I want to display the 'lat' and 'lng' from the 'southwest' or I want to display the 'text'. 对于样本,我想显示“西南”中的“纬度”和“ lng”,或者我想显示“文本”。
{ "routes" : [ { "bounds" : { "northeast" : { "lat" : -26.758340, "lng" : 153.035930 }, "southwest" : { "lat" : -27.454070, "lng" : 152.852150 } }, "copyrights" : "Map data ©2011 GBRMPA, Google, Whereis(R), Sensis Pty Ltd", "legs" : [ { "distance" : { "text" : "89.7 km", "value" : 89693 }, "duration" : { "text" : "1 hour 17 mins", "value" : 4633 }, "end_address" : "Maleny QLD 4552, Australia", "end_location" : { "lat" : -26.758420, "lng" : 152.852150 }, "start_address" : "Brisbane QLD, Australia", "start_location" : { "lat" : -27.454070, "lng" : 153.026880 }, } ], } ] } {“ routes”:[{“ bounds”:{“ northeast”:{“ lat”:-26.758340,“ lng”:153.035930},“ southwest”:{“ lat”:-27.454070,“ lng”:152.852150}} ,“版权”:“地图数据©2011 GBRMPA,Google,Sheres Pty Ltd,Google”,“ legs”:[{“ distance”:{“ text”:“ 89.7 km”,“ value”:89693} ,“ duration”:{“ text”:“ 1小时17分钟”,“ value”:4633},“ end_address”:“ Maleny QLD 4552,Australia”,“ end_location”:{“ lat”:-26.758420,“ lng “:152.852150},” start_address“:”澳大利亚布里斯班昆士兰州“,” start_location“:{” lat“:-27.454070,” lng“:153.026880},}],}]}
Cheers! 干杯!
Assuming that data is your json, you can use $.parseJSON(data) to get the object. 假设数据是您的json,则可以使用$ .parseJSON(data)获取对象。
By the way, the json in your example is not valid - you have two extra comas near the end. 顺便说一句,示例中的json无效-末尾有两个额外的逗号。 Here's an example using the valid json: 这是使用有效json的示例:
var data = '{ "routes" : [ { "bounds" : { "northeast" : { "lat" : -26.758340, "lng" : 153.035930 }, "southwest" : { "lat" : -27.454070, "lng" : 152.852150 } }, "copyrights" : "Map data ©2011 GBRMPA, Google, Whereis(R), Sensis Pty Ltd", "legs" : [ { "distance" : { "text" : "89.7 km", "value" : 89693 }, "duration" : { "text" : "1 hour 17 mins", "value" : 4633 }, "end_address" : "Maleny QLD 4552, Australia", "end_location" : { "lat" : -26.758420, "lng" : 152.852150 }, "start_address" : "Brisbane QLD, Australia", "start_location" : { "lat" : -27.454070, "lng" : 153.026880 } } ] } ] }';
var routes = $.parseJSON(data).routes;
Now routes is a javascript array of route objects. 现在,路线是路线对象的javascript数组。 For example routes[0].copyrights
returns "Map data ©2011 GBRMPA, Google, Whereis(R), Sensis Pty Ltd". 例如, routes[0].copyrights
返回“地图数据©2011 GBRMPA,Google,Sensis Pty Ltd”。 Hope this helps! 希望这可以帮助!
As Georgi said, use $.parseJSON(data)
. 正如Georgi所说,请使用$.parseJSON(data)
。
Then, to access the lat
and lng
properties, you can use the dot notation: 然后,要访问lat
和lng
属性,可以使用点表示法:
var data = ''; // json string here
var jsonObject = $.parseJSON(data);
var lat = jsonObject.routes[0].bounds.northeast.lat;
var lng = jsonObject.routes[0].bounds.northeast.lng;
Because routes
is an array object, we use the regular array element accessor ( []
) to access the first element ( 0
). 因为routes
是一个数组对象,所以我们使用常规的数组元素访问器( []
)访问第一个元素( 0
)。
it is possible that your json string is already parsed, you can get a json object from an ajax service, if you then want a value of a certain json object or sub object you can just fetch it like this: 可能已经解析了您的json字符串,您可以从ajax服务获取一个json对象,如果您想要某个json对象或子对象的值,则可以像这样获取它:
var data = {here goes your whole json object you have above...};
var southwestLong = data.routes[0].bounds.southwest.lng;
basically, the parsing is left out because it came in already as a json object. 基本上,分析被省略了,因为它已经作为json对象进入了。
but in most cases you would be writing some looping for example loop over all route objects, to render them in a list. 但在大多数情况下,您可能会编写一些循环,例如在所有路由对象上循环,以将它们呈现在列表中。
Also, I advise you to test for a certain item to be defined. 另外,我建议您测试要定义的特定项目。 because if you pass in a changed json object, where it has 0 routes for example, the above code would error, because data.routes[0]
does not exist. 因为如果传入一个更改后的json对象(例如,它具有0条路由),则上述代码将出错,因为data.routes[0]
不存在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.