how to read json result in jquery?

前端 未结 4 1204
忘掉有多难
忘掉有多难 2020-11-27 22:43

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 ex

相关标签:
4条回答
  • 2020-11-27 23:07
    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
    
    0 讨论(0)
  • 2020-11-27 23:09
    var json = $.parseJson(jsonString);
    

    To get the value 282829899 for "HAWBID", you would use:

    var hawbid = json.waybill_log.TrackingResult.HAWBEntity.HAWBID;
    
    0 讨论(0)
  • 2020-11-27 23:10

    If you're using jQuery to get the JSON, you can just use:

    http://api.jquery.com/jquery.getjson/

    And it will parse the JSON for you.

    0 讨论(0)
  • 2020-11-27 23:14

    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);
    
    0 讨论(0)
提交回复
热议问题