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); returns 0.00. I have never dealt with C structs before and not sure if I setup the syntax correctly. My json structure seems fairly straight forward; however, I am not sure if I am accessing the correct creatObject() for the ph float value. What should I do to get the proper value of ph?
For further information about the aJSON library, please visit: http://interactive-matter.eu/blog/2010/08/14/ajson-handle-json-with-arduino/
EDIT new code:
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);
I ended up changing the json structure to more of a simpler structure. I felt there wasn’t a need for a another substructure due to the ram shortage of the arduino. After shortening the json structure I was able to successfully get the valuefloat of ph. I am still interested in figuring out the prior issue for future reference.
I just tried your code and found that you have declared both sBoard and sensorType twice at the beginning of the snippet.
I removed it and I am getting the proper response “5.33”. This is the code that I am using after editing.