更新时间:2023-01-16 14:16:08
使用jQuery的 jQuery.parseJSON()
从JSON-String中获取JavaScript对象的方法:
Use jQuery's jQuery.parseJSON()
method to get a JavaScript object out of your JSON-String:
var test = jQuery.parseJSON(data); // Where 'data' is your JSON string
解析后, test
是一个JavaScript对象。关于 parseJSON()
的 jQuery docs :
After parsing, test
is a JavaScript object. The jQuery docs about parseJSON()
:
jQuery.parseJSON()
获取格式正确的JSON字符串并返回生成的JavaScript
对象。
...
Takes a well-formed JSON string and returns the resulting JavaScript object. ...
关于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
}
那里有两种访问属性的可能性(但不是方法,见下文),所谓的 物业访问者 :
There are two possibilities to access properties (but not methods, see below), so called Property Accessor:
- 点符号:
使用点符号,你可以n访问属性&方法
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
或
window.console.log(test.waybill_log.TrackingResult.HAWBEntity);
// Should give something like: Object { HAWBID: '282829899'}
或
window.console.log(test.waybill_log.HAWBItemEntity);
// null