-
使用 jQuery 的jQuery.parseJSON()
从 JSON 字符串中获取 JavaScript 对象的方法:
var test = jQuery.parseJSON(data); // Where 'data' is your JSON string
解析之后,test
是一个 JavaScript 对象。这jQuery 文档 http://api.jquery.com/jquery.parsejson/ about parseJSON()
:
jQuery.parseJSON()
获取格式正确的 JSON 字符串并返回生成的 JavaScript
目的。
...
关于 Javascript 对象:
// 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
}
有两种访问属性的可能性(但不是方法,见下文),所谓属性存取器 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_Accessors:
- “点表示法”:
使用点符号,您可以访问属性和方法
var objOne = new Obj(); // Create a new instance of Obj
objOne.propertyTwo; // 52.3654
var objTwo = new Obj(); // Another instance of Obj
objTwo.propertyThtree.propTrheeProperty; // 42
objTwo.propertyThtree.propTrheeAnotherProperty; // whatever
// Accessing methods
objOne.methodOne(); // whatever your function methodOne() returns or does
objTwo.methodTwo(); // whatever your function methodTwo() returns or does
- “括号表示法”:
使用括号表示法,您还可以访问属性和方法
objTwo['propertyThtree']['propTrheeProperty']; // 42
objOne['methodOne']();
代替
objTwo.propertyThtree.propTrheeProperty; // 42
objOne.methodOne();
在你的情况下,这意味着:
window.console.log(test.waybill_log.TrackingResult.HAWBEntity.HAWBID);
// 282829899
Or
window.console.log(test.waybill_log.TrackingResult.HAWBEntity);
// Should give something like: Object { HAWBID: '282829899'}
Or
window.console.log(test.waybill_log.HAWBItemEntity);
// null