您现在的位置是:网站首页> 硬件

Ardunio解析判断是否有key和数组的解析

  • 硬件
  • 2021-12-22
  • 787人已阅读
摘要

Serial.begin(115200);

  DynamicJsonDocument doc(1024);


  // You can use a String as your JSON input.

  // WARNING: the string in the input  will be duplicated in the JsonDocument.

  String input =

      "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038],\"array\":[{\"A\":12},{\"A\":14}]}";

  deserializeJson(doc, input);

  JsonObject obj = doc.as<JsonObject>();


  // You can use a String to get an element of a JsonObject

  // No duplication is done.

  long time = obj[String("time")];

  const char *ss=obj["aa"]; //判断aa键是否存在

  if(!ss)

  {

    Serial.printf("aa is null\r\n");

  }

  else

  {

    Serial.printf("aa is not null\r\n");

  }


  JsonArray arr = obj["array"].as<JsonArray>();

  if(arr.isNull())

  {

     Serial.printf("arr is  null\r\n");

  }

  else

  {

    int nSize=arr.size();

    Serial.printf("arr size is  %d\r\n",nSize);

    int A=arr[0]["A"].as<int>();

    Serial.printf("arr[0].A is  %d\r\n",A);

  }


Top