如何通过java脚本解析json数据和区分所需的值

时间:2017-07-20 17:16:41

标签: javascript json postman

我有json数据,它有类似的变量,我正在寻找从中获取所需的id值。我尝试了很多格式来检查/验证数据,但由于可用的变量相同,我无法检索所需的解决方案。

[
  {
    "id": "",
    "email": ,
    "name": "",
    "phone": "",
    "shippingAddress": {
      "address1": "",
      "city": ,
      "country": ,
      "postalCode": ,
      "state": 
    },
    "billingAddress": {
      "address1": "",
      "city": "",
      "country": ,
      "postalCode": ,
      "state": ""
    }
  },
  {
    "id": "",
    "email": ,
    "name": "",
    "phone": "",
    "shippingAddress": {
      "address1": ,
      "city": ,
      "country": ,
      "postalCode": ,
      "state": 
    },
    "billingAddress": {
      "address1": " ",
      "city": "",
      "country": "",
      "postalCode": "",
      "state": ""
    }
  }]

我尝试了类似的算法。

for (var i=0; i<jsonData.length; i++){
  tests["Checking shippingAddress"] = jsonData.shippingAddress.address == " Constitution ";   
}

我在Postman中为Api做自动化,所以api有可用相同变量的json数据。当单个数据时,代码工作正常。

1 个答案:

答案 0 :(得分:0)

您可能在如何迭代数据时遇到问题。

for (var i=0; i < jsonData.length; i++){
  tests["Checking shippingAddress"] = jsonData[i].shippingAddress.address === " Constitution ";   
}