使用ArduinoJson 6创建Json对象

hi3rlvi2  于 2023-02-26  发布在  其他
关注(0)|答案(1)|浏览(236)

所以我尝试用4个相同的对象来创建和json,但是似乎不能用ArduinoJson 6来工作,版本5看起来更容易使用.createObject,但是很好奇为什么他们在6中删除了它,因为一定还有办法做到这一点。有办法做到这一点吗?或者我必须降级到版本5吗?
我想让它看起来像:

{
   "Spot":[
      {
         "LocationId":"673d855c-9f66-4e49-8b2c-737e829d880c",
         "SpotId":"1",
         "Occupied":"Empty"
      },
      {
         "LocationId":"673d855c-9f66-4e49-8b2c-737e829d880c",
         "SpotId":"2",
         "Occupied":"Empty"
      },
      {
         "LocationId":"673d855c-9f66-4e49-8b2c-737e829d880c",
         "SpotId":"3",
         "Occupied":"Empty"
      },
      {
         "LocationId":"673d855c-9f66-4e49-8b2c-737e829d880c",
         "SpotId":"4",
         "Occupied":"Empty"
      }
   ]
}

我的代码:

char json_string[256];
  StaticJsonDocument<256> carparkResponse;

  JsonObject spot1 = carparkResponse.createNestedObject("Spot");
   spot1["LocationId"] = LOCATION_ID;
   spot1["SpotId"] = "1";
   spot1["Occupied"] = occupied1.isOccupied();
   
  JsonObject spot2 = spot1.createNestedObject();
  spot2["LocationId"] = LOCATION_ID;
  spot2["SpotId"] = "2";
  spot2["Occupied"] = occupied2.isOccupied();
  
  JsonObject spot3 = spot2.createNestedObject();
  spot3["LocationId"] = LOCATION_ID;
  spot3["SpotId"] = "3";
  spot3["Occupied"] = occupied3.isOccupied();
  
  JsonObject spot4 = spot3.createNestedObject();
  spot4["LocationId"] = LOCATION_ID;
  spot4["SpotId"] = "4";
  spot4["Occupied"] = occupied4.isOccupied();
  

  serializeJson(carparkResponse, json_string);
  Serial.println(json_string);
3xiyfsfu

3xiyfsfu1#

所以我找到了Json构建器,它解决了我的问题,以防其他人必须创建任何Json的https://arduinojson.org/v6/assistant/#/step1

相关问题