如何使用jquery或javascript获取JSON中的对象 [英] How can I get the object in JSON using jquery or javascript

查看:80
本文介绍了如何使用jquery或javascript获取JSON中的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是JSON新手

如何将以下数据显示到html.例如,我想显示西南"的纬度"和"lng",或者我想显示文本".

How can I display the following data to the html. for sample I want to display the 'lat' and 'lng' from the 'southwest' or I want to display the 'text'.

{路线":[ { 界限":{ 东北":{ "lat":-26.758340, "lng":153.035930 }, 西南":{ "lat":-27.454070, "lng":152.852150 } }, 版权":地图数据©2011 GBRMPA,Google,Whereis(R), Sensis Pty Ltd", 腿":[ { 距离" : { 文字":"89.7公里", 值":89693 }, 期间" : { "text":"1小时17分钟", 值":4633 }, "end_address":澳大利亚的Maleny QLD 4552", "end_location":{ "lat":-26.758420, "lng":152.852150 }, "start_address":澳大利亚布里斯班昆士兰州", "start_location":{ "lat":-27.454070, "lng":153.026880 }, } ], } ]}

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

干杯!

推荐答案

正如Georgi所说,请使用 .

As Georgi said, use $.parseJSON(data).

然后,要访问latlng属性,可以使用点符号:

Then, to access the lat and lng properties, you can use the dot notation:

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;

由于routes是数组对象,因此我们使用常规数组元素访问器([])访问第一个元素(0).

Because routes is an array object, we use the regular array element accessor ([]) to access the first element (0).

这篇关于如何使用jquery或javascript获取JSON中的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆