Javascript 如何在jquery中读取json结果?
我不熟悉jquery。你能帮我吗? 我有一个来自url的json响应,但我不知道如何读取jquery中的键值 例如,如何获取“HawBitementy”值? 请检查下面的json响应Javascript 如何在jquery中读取json结果?,javascript,jquery,json,getjson,Javascript,Jquery,Json,Getjson,我不熟悉jquery。你能帮我吗? 我有一个来自url的json响应,但我不知道如何读取jquery中的键值 例如,如何获取“HawBitementy”值? 请检查下面的json响应 { "waybill_log": { "TrackingResult": { "HAWBEntity": { "HAWBID": 282829899, }, "HAWBHistoryEntity": [ { "Act
{
"waybill_log": {
"TrackingResult": {
"HAWBEntity": {
"HAWBID": 282829899,
},
"HAWBHistoryEntity": [
{
"ActionDate": "4/26/2014 12:32:00 PM",
},
{
"ActionDate": "4/26/2014 12:32:00 PM",
}
],
"HAWBAttachmentEntity": [
{
"FileName": "Invoice_30018018516..pdf",
}
],
"HAWBItemEntity": null,
},
"HAWBAttachmentEntityExtendedList": [
{
"HAWBAttachmentEntity": {
"FileName": "Invoice_30018018516..pdf",
},
"AttachmentLink": "nw"
}
],
"CurrentStatus": "Delivery",
"ConsolsData": {
"ConsolNumber": null,
},
"ItemContainerData": {
"ContainerNumber": null,
},
"FlightDetails": null,
}
}
jQuery.parseJSON()
方法从JSON字符串中获取JavaScript对象:
var test = jQuery.parseJSON(data); // Where 'data' is your JSON string
test
是一个JavaScript对象。关于parseJSON()
:// Declaration
var Obj = {
// Properties:
propertyOne: 'value', // string
propertyTwo: 52.3654, // float
// propertyThree is an object inside 'Obj'
// defined by the braces
// which may naturally contain its own properties & methods
propertyThree: {
propTrheeProperty: 42, // int
propTrheeAnotherProperty: 'whatever',
thePropThreeMethod: function () {
// your function code
}
// and so on, no coma after the last property/method
},
// and so on
// 'Obj' - Methods:
methodOne: function () {
// your function code
},
methodTwo: function () {
// your function code
}
// and so on, no coma after the last property/method
}
window.console.log(test.waybill_log.TrackingResult.HAWBEntity.HAWBID);
// 282829899
访问属性(但不是方法,见下文)有两种可能性,即:
“点符号”:
// Declaration
var Obj = {
// Properties:
propertyOne: 'value', // string
propertyTwo: 52.3654, // float
// propertyThree is an object inside 'Obj'
// defined by the braces
// which may naturally contain its own properties & methods
propertyThree: {
propTrheeProperty: 42, // int
propTrheeAnotherProperty: 'whatever',
thePropThreeMethod: function () {
// your function code
}
// and so on, no coma after the last property/method
},
// and so on
// 'Obj' - Methods:
methodOne: function () {
// your function code
},
methodTwo: function () {
// your function code
}
// and so on, no coma after the last property/method
}
window.console.log(test.waybill_log.TrackingResult.HAWBEntity.HAWBID);
// 282829899
使用点表示法,您可以访问属性和方法
“括号符号”:
// Declaration
var Obj = {
// Properties:
propertyOne: 'value', // string
propertyTwo: 52.3654, // float
// propertyThree is an object inside 'Obj'
// defined by the braces
// which may naturally contain its own properties & methods
propertyThree: {
propTrheeProperty: 42, // int
propTrheeAnotherProperty: 'whatever',
thePropThreeMethod: function () {
// your function code
}
// and so on, no coma after the last property/method
},
// and so on
// 'Obj' - Methods:
methodOne: function () {
// your function code
},
methodTwo: function () {
// your function code
}
// and so on, no coma after the last property/method
}
window.console.log(test.waybill_log.TrackingResult.HAWBEntity.HAWBID);
// 282829899
使用括号表示法,还可以访问属性和方法
而不是
objTwo.propertyThtree.propTrheeProperty; // 42
objOne.methodOne();
在您的情况下,这意味着:
// Declaration
var Obj = {
// Properties:
propertyOne: 'value', // string
propertyTwo: 52.3654, // float
// propertyThree is an object inside 'Obj'
// defined by the braces
// which may naturally contain its own properties & methods
propertyThree: {
propTrheeProperty: 42, // int
propTrheeAnotherProperty: 'whatever',
thePropThreeMethod: function () {
// your function code
}
// and so on, no coma after the last property/method
},
// and so on
// 'Obj' - Methods:
methodOne: function () {
// your function code
},
methodTwo: function () {
// your function code
}
// and so on, no coma after the last property/method
}
window.console.log(test.waybill_log.TrackingResult.HAWBEntity.HAWBID);
// 282829899
或
或
如果您使用jQuery获取JSON,您可以使用: 它将为您解析JSON
var json = $.parseJson(jsonString);
要获得“HAWBID”的值282829899,您可以使用:
var hawbid = json.waybill_log.TrackingResult.HAWBEntity.HAWBID;
一般来说,您不必使用jquery阅读json 只需使用
JSON.parse()
函数,不使用Jquery,轻松阅读即可
var json = '{"result":true,"count":1}',
obj = JSON.parse(json);
alert(obj.count);
如果答案对你有帮助,你能接受吗?对你们,对其他回答者来说,这真是太好了,他们花时间帮助你们,最后但并非最不重要的是,这会让我也很高兴还有,我知道你从不接受答案。。。真可悲