从字符串中解析JSON数据

时间:2017-12-24 06:01:16

标签: json http arduino

我在Arduino中使用HTTP方法获取JSON数据并存储在String对象中。数据是:

{
  "item": {
    "Identity": {
      "Id": "327681",
      "ItemId": "64006A962B71A1E7B3A0428637DA997C.327681",
      "Level": 1,
      "EntityType": "64006A962B71A1E7B3A0428637DA997C",
      "ItemStatus": 1
    },
    "Properties": {
      "AssetName": "PHE-1001",
      "Category": "Electrical Appliance",
      "RegistrationTime": "2017-12-14Z",
      "Activated": true,
      "Status": "Offline",
      "Manufacturer": "Philips",
      "ModelNumber": "1E-S00ER11",
      "SerialNumber": "YGTJGJK458545",
      "sample_property": null,
      "AssetLocation": null,
      "AssetType": null,
      "ActivationTime": "2017-12-24T05:44:38Z",
      "Country": "India",
      "PostalAddress": "500081",
      "dummy": null,
      "TotalHours": 16,
      "TotalWorkingHoursFromInstallation": 38,
      "TotalLifeTime": 62,
      "AssetSensorDistance": null
    }
  }
}

Arduino代码:

HTTPClient http;
http.begin("URL");
int httpCode = http.GET(); //  //Send the request
if (httpCode == 200) {
  String payload = http.getString();
  Serial.println(payload);
}

现在我想只获得AssetNameStatusAssetSensorDistance。我尝试了payload["Status"],但没有打印任何内容。

任何人都可以帮我吗?提前谢谢。

1 个答案:

答案 0 :(得分:1)

你在这里缺少一些重要的内容。

  • 您需要包含ArduinoJson库

  • 您需要使用JsonBuffer将字符串实际解析为JsonObject

  • 状态路径为yourRootObject [“Properties”] [“Status”],因为它包含在您的属性中。

见这里:https://arduinojson.org/doc/decoding/

祝你好运!