Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
244 views
in Technique[技术] by (71.8m points)

javascript - how to read json result in jquery?

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,
  }

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  1. 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
    
  2. After parsing, test is a JavaScript object. The jQuery docs about parseJSON():

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 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:

- 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...