Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/435.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在jquery中读取json结果?_Javascript_Jquery_Json_Getjson - Fatal编程技术网

Javascript 如何在jquery中读取json结果?

Javascript 如何在jquery中读取json结果?,javascript,jquery,json,getjson,Javascript,Jquery,Json,Getjson,我不熟悉jquery。你能帮我吗? 我有一个来自url的json响应,但我不知道如何读取jquery中的键值 例如,如何获取“HawBitementy”值? 请检查下面的json响应 { "waybill_log": { "TrackingResult": { "HAWBEntity": { "HAWBID": 282829899, }, "HAWBHistoryEntity": [ { "Act

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

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

请检查下面的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,
  }

}
  • 使用jQuery的
    jQuery.parseJSON()
    方法从JSON字符串中获取JavaScript对象:

    var test = jQuery.parseJSON(data); // Where 'data' is your JSON string
    
  • 解析后,
    test
    是一个JavaScript对象。关于
    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
    }
    
    window.console.log(test.waybill_log.TrackingResult.HAWBEntity.HAWBID); 
    // 282829899
    
    访问属性(但不是方法,见下文)有两种可能性,即:

    “点符号”:

    // 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
    }
    
    window.console.log(test.waybill_log.TrackingResult.HAWBEntity.HAWBID); 
    // 282829899
    
    使用点表示法,您可以访问属性和方法

    “括号符号”:

    // 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
    }
    
    window.console.log(test.waybill_log.TrackingResult.HAWBEntity.HAWBID); 
    // 282829899
    
    使用括号表示法,还可以访问属性和方法

    而不是

    objTwo.propertyThtree.propTrheeProperty; // 42
    objOne.methodOne();
    

    在您的情况下,这意味着:

    // 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
    }
    
    window.console.log(test.waybill_log.TrackingResult.HAWBEntity.HAWBID); 
    // 282829899
    


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

    它将为您解析JSON

    var json = $.parseJson(jsonString);
    
    要获得“HAWBID”的值282829899,您可以使用:

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

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

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

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

    如果答案对你有帮助,你能接受吗?对你们,对其他回答者来说,这真是太好了,他们花时间帮助你们,最后但并非最不重要的是,这会让我也很高兴还有,我知道你从不接受答案。。。真可悲