1
  aJsonObject* sBoard = aJson.createObject();
  aJsonObject* sensorType = aJson.createObject();

  aJsonObject* sBoard = aJson.createObject();
  aJsonObject* sensorType = aJson.createObject();

  aJson.addItemToObject(sBoard, "name", aJson.createItem("SensorBoardOne"));
  aJson.addItemToObject(sBoard, "Sensor", sensorType);

  aJson.addNumberToObject(sensorType, "ph", 5.33);
  aJson.addNumberToObject(sensorType, "ec", 880);
  aJson.addNumberToObject(sensorType, "temp", 74.68);

  aJsonObject* ph = aJson.getObjectItem(sensorType, "ph");

  Serial.println(ph->valuefloat);

Serial.println(ph->valuefloat);返回 0.00。我以前从未处理过 C 结构,并且不确定我是否正确设置了语法。我的 json 结构看起来相当简单;但是,我不确定我是否正在为 ph 浮点值访问正确的 creatObject()。我应该怎么做才能获得正确的ph值?

有关 aJSON 库的更多信息,请访问:http: //interactive-matter.eu/blog/2010/08/14/ajson-handle-json-with-arduino/

编辑新代码:

  aJsonObject* sBoard = aJson.createObject();
  
  aJson.addItemToObject(sBoard, "name", aJson.createItem("SensorBoardOne"));
  
  aJson.addNumberToObject(sBoard, "ph", 5.33);
  aJson.addNumberToObject(sBoard, "ec", 880);
  aJson.addNumberToObject(sBoard, "temp", 74.68);
  
  aJsonObject* phValue = aJson.getObjectItem(sBoard, "ph");
  char* string = aJson.print(sBoard);
    if (string != NULL) {
      Serial.println(string); //prints out: {"name":"SensorBoardOne","ph":5.33000,"ec":880,"temp":74.68000}

    }

  Serial.println(phValue->valuefloat);

我最终将 json 结构更改为更简单的结构。由于 arduino 的内存不足,我觉得不需要另一个子结构。缩短 json 结构后,我能够成功获得 ph 的 valuefloat。我仍然有兴趣找出以前的问题以供将来参考。

4

1 回答 1

0

我刚刚尝试了您的代码,发现您在代码段的开头两次声明了 sBoard 和 sensorType 。

我删除了它,我得到了正确的响应“5.33”。这是我在编辑后使用的代码。

  aJsonObject* sBoard = aJson.createObject();
  aJsonObject* sensorType = aJson.createObject();

  aJson.addItemToObject(sBoard, "name", aJson.createItem("SensorBoardOne"));
  aJson.addItemToObject(sBoard, "Sensor", sensorType);

  aJson.addNumberToObject(sensorType, "ph", 5.33);
  aJson.addNumberToObject(sensorType, "ec", 880);
  aJson.addNumberToObject(sensorType, "temp", 74.68);

  aJsonObject* ph = aJson.getObjectItem(sensorType, "ph");

  Serial.println(ph->valuefloat);
于 2012-08-04T15:33:26.897 回答