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 float值的正确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的ram短缺,不需要另一个子结构。缩短json结构后,我能够成功获得ph值的值。我仍然有兴趣找出先前的问题以供将来参考。
答案 0 :(得分: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);