如何在jquery中读取json结果?

IT技术 javascript jquery json getjson
2021-02-27 20:57:08

我不熟悉 jquery。你能帮我吗?我有一个来自 url 的 json 响应,但我不知道如何,我可以读取 jquery 中的键值吗?

例如,如何获取“HAWBItemEntity”值?

请检查下面的 json 响应。

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

}
4个回答
  1. 使用 jQuery 的jQuery.parseJSON()方法从 JSON-String 中获取 JavaScript 对象:

    var test = jQuery.parseJSON(data); // Where 'data' is your JSON string
    
  2. 解析后,test是一个JavaScript对象。jQuery的文档有关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
}

有两种访问属性的可能性(但不是方法,见下文),所谓的属性访问器

- “点符号”:

使用点符号,您可以访问属性和方法

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

一般来说,您不必使用 jquery 读取 json。

只需轻松阅读,使用JSON.parse()函数而不使用 Jquery。

var json = '{"result":true,"count":1}',
    obj = JSON.parse(json);

alert(obj.count);

如果您使用 jQuery 获取 JSON,则可以使用:

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

它将为您解析 JSON。

var json = $.parseJson(jsonString);

要获得“HAWBID”的值 282829899,您可以使用:

var hawbid = json.waybill_log.TrackingResult.HAWBEntity.HAWBID;