admin管理员组文章数量:1420197
I'm not familiar with jquery. Will you please help me in this? I have a json respone from url but i don't know how, can i read key value in jquery.
For example, how to get the "HAWBItemEntity" value?
Please check the below json-response.
{
"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,
}
}
I'm not familiar with jquery. Will you please help me in this? I have a json respone from url but i don't know how, can i read key value in jquery.
For example, how to get the "HAWBItemEntity" value?
Please check the below json-response.
{
"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,
}
}
Share
Improve this question
edited Feb 2, 2016 at 13:02
Daniel Gasser
5,1538 gold badges46 silver badges63 bronze badges
asked Apr 26, 2014 at 11:41
Rohit KhuranaRohit Khurana
2693 gold badges9 silver badges18 bronze badges
1
- Could you accept an answer if it helped you? Would be really nice for you, for other answerers, who spent time helping you and last but not least, it would make me happy too. ;-) Also, I see that you never accept an answer... that's sad – Daniel Gasser Commented Sep 5, 2014 at 15:02
4 Answers
Reset to default 4Use 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
After parsing,
test
is a JavaScript object. The jQuery docs aboutparseJSON()
:
jQuery.parseJSON()
Takes a well-formed JSON string and returns the resulting JavaScript object. ...
About the Javascript Object:
// 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 a after the last property/method
},
// and so on
// 'Obj' - Methods:
methodOne: function () {
// your function code
},
methodTwo: function () {
// your function code
}
// and so on, no a after the last property/method
}
There are two possibilities to access properties (but not methods, see below), so called Property Accessor:
- The "Dot Notation":
With the dot notation, you can access properties & methods
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
- The "Bracket Notation":
With the bracket notation, you can also access properties & methods
objTwo['propertyThtree']['propTrheeProperty']; // 42
objOne['methodOne']();
instead of
objTwo.propertyThtree.propTrheeProperty; // 42
objOne.methodOne();
In your case, that means:
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
You don't have to read json with jquery in general .
Just read it with ease , using JSON.parse()
function and without Jquery .
var json = '{"result":true,"count":1}',
obj = JSON.parse(json);
alert(obj.count);
If you're using jQuery to get the JSON, you can just use:
http://api.jquery./jquery.getjson/
And it will parse the JSON for you.
var json = $.parseJson(jsonString);
To get the value 282829899 for "HAWBID", you would use:
var hawbid = json.waybill_log.TrackingResult.HAWBEntity.HAWBID;
本文标签: javascripthow to read json result in jqueryStack Overflow
版权声明:本文标题:javascript - how to read json result in jquery? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745323974a2653505.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论