简体   繁体   English

如何使用jquery或javascript在JSON中获取对象

[英]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: 然后,要访问latlng属性,可以使用点表示法:

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.

相关问题 jQuery / Json-如何获得最后一个 <P> 在这个JSON对象中? - Jquery/Json - How can I get the last <P> in this JSON object? 如何获取PHP JSON编码的对象作为Javascript / JQuery上的变量? - How can I get a PHP JSON-encoded object as a variable on Javascript/JQuery? How can i convert my string output into the object so that i get json as a output using javascript? - How can i convert my string output into the object so that i get json as a output using javascript? 如何使用jquery / javascript将值插入javascript对象 - How can I insert values in to javascript object by using jquery / javascript 如何通过在单击事件中使用javascript / jquery一一从json中获取数据? - How can I get the data from the json one by one by using javascript/jquery on click event? 我如何使用jquery或javascript从json内的列表中获取信息? - How can I get information from a list inside json using jquery or javascript? 如何使用 JQUERY 从 JSON 数组对象获取数据? - How can I get data from JSON array object with JQUERY? 我如何在JavaScript中获取一对json对象 - How can i get pair of the json object in javascript 如何从 JavaScript 中的 json 脚本中获取 object - How can I get an object from a json script in JavaScript 如何使用Javascript或Jquery将JSON对象包装在数组中? - How can I wrap JSON objects in an array using Javascript or Jquery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM