A recipe to fix any error is to investigate.
I'm absolutely certain (I have checked ) the ArduinoJson library features an example that shows how to properly get a string from a JsonVariant object.
A recipe to fix any error is to investigate.
I'm absolutely certain (I have checked ) the ArduinoJson library features an example that shows how to properly get a string from a JsonVariant object.
as mentioned by @ScruffR, all you need is here: arduinojson.org/v5/example/parser/
This doesn’t seem to be valid JSON.
The keys need to be strings (wrapped in double quotes) and each individual field in your array ([...]
) should be wrapped in curly braces ( [ {"0":"PADDY"}, {"1":"JOWAR"}, ...]
)
You should feed your pseudo JSON into a online parser to get that sorted before moving on.
I was able to compile the test code with no errors for my argon. let’s say that you fix your json and it’s looks similar to this:
{
“columns”: {
“crops”: [
{“0”:“PADDY”},
{“1”:“JOWAR”},
{“2”:“BAJRA”},
{“3”:“MAIZE”}],
“rates”: [
{“0”:1750},
{“1”:2430},
{“2”:1950},
{“3”:1700}]
}
}
// This #include statement was automatically added by the Particle IDE.
#include <SparkJson.h>
const size_t capacity = 2*JSON_ARRAY_SIZE(4) + 9*JSON_OBJECT_SIZE(1) + JSON_OBJECT_SIZE(2) + 90;
StaticJsonBuffer<capacity> jsonBuffer;
char* json = "{\"columns\":{\"crops\":[{\"0\":\"PADDY\"},{\"1\":\"JOWAR\"},{\"2\":\"BAJRA\"},{\"3\":\"MAIZE\"}],\"rates\":[{\"0\":1750},{\"1\":2430},{\"2\":1950},{\"3\":1700}]}}";
void setup() {
}
void loop() {
JsonObject& root = jsonBuffer.parseObject(json);
JsonArray& cropsArray = root["columns"]["crops"];
JsonArray& ratesArray = root["columns"]["rates"];
const char* columns_crops_0_0 = cropsArray[0]["0"]; // "PADDY"
Serial.println(columns_crops_0_0);
const char* columns_crops_1_1 = cropsArray[1]["1"]; // "JOWAR"
Serial.println(columns_crops_1_1);
const char* columns_crops_2_2 = cropsArray[2]["2"]; // "BAJRA"
Serial.println(columns_crops_2_2);
const char* columns_crops_3_3 = cropsArray[3]["3"]; // "MAIZE"
Serial.println(columns_crops_3_3);
int columns_rates_0_0 = ratesArray[0]["0"]; // 1750
Serial.println(columns_rates_0_0);
int columns_rates_1_1 = ratesArray[1]["1"]; // 2430
Serial.println(columns_rates_0_0);
int columns_rates_2_2 = ratesArray[2]["2"]; // 1950
Serial.println(columns_rates_0_0);
int columns_rates_3_3 = ratesArray[3]["3"]; // 1700
// or.......
for(JsonObject& result : cropsArray){
JsonObject& crops = result;
Serial.println(crops["0"].as<const char*>()); // "PADDY"
Serial.println(crops["1"].as<const char*>()); // "JOWAR"
Serial.println(crops["2"].as<const char*>()); // "BAJRA"
Serial.println(crops["3"].as<const char*>()); // "MAIZE"
}
for(JsonObject& result : ratesArray){
JsonObject& rates = result;
Serial.println(rates["0"].as<int>()); // 1750
Serial.println(rates["1"].as<int>()); // 2430
Serial.println(rates["2"].as<int>()); // 1950
Serial.println(rates["3"].as<int>()); // 1700
}
}
I used https://arduinojson.org/v5/assistant/ which is preatty usefull tools. and https://arduinojson.org/v5/doc/decoding/ to learn how
to deal with json.
Please note that I did not tested / flashed this code to my argon !!!
Use a logic level shifter. that is the first thing that you should be doing. They are inexpensive and they will save your project. If you do not know how to set one up, that is OK, we can show you. With 5v and 3.3v, you risk destroying your project. at higher voltages and larger currents, you risk fire.
Please note, i have no idea what your lcd pins should be, so they are just attached where it looked convenient. Also, I do not have a fritzing model for the xenon, only the photon and electron. You can set the pinouts to your xenon just by the pin name.
I am unable to fix my json. Will this be a valid one.`
{"rows":[{"crops":"PADDY","rates":1750},{"crops":"JOWAR","rates":2430},{"crops":"BAJRA","rates":1950},{"crops":"MAIZE","rates":1700},{"crops":"RAGI","rates":2897},{"crops":"ARHAR(Tur)","rates":5675},{"crops":"MOONG","rates":6975},{"crops":"URAD","rates":5600},{"crops":"COTTON","rates":5150},{"crops":"GROUNDNUT IN SHELL","rates":4890},{"crops":"SUNFLOWER SEED","rates":5388},{"crops":"SOYABEAN","rates":3399},{"crops":"SESAMUM","rates":6249},{"crops":"NIGERSEED","rates":5877},{"crops":"WHEAT","rates":1840},{"crops":"BARLEY","rates":1440},{"crops":"GRAM","rates":4620},{"crops":"MASUR (LENTIL)","rates":4475},{"crops":"RAPESEED/MUSTARD","rates":4200},{"crops":"SAFFLOWER","rates":4945},{"crops":"TORIA","rates":4190},{"crops":"JUTE","rates":3700},{"crops":"SUGARCANE $","rates":275}]}
This is valid.
Use https://jsonlint.com to validate any Json. documents
I made one more test code which successful parse jour new json and flashed my Argon then publish results witch Particle.publish() . All works perfect !. Please note i’m not a programmer ! I even never “stood beside any professional one”, so the code is just for demonstrating the idea how to deal with json also parseFunc() will block for 24s maybe @ScruffR can add/correct something whch supose to be done in diffrent/easiest way.
// This #include statement was automatically added by the Particle IDE.
#include <SparkJson.h>
#include "Particle.h"
unsigned long interval = 5000;
char* json = "{\"rows\":[{\"crops\":\"PADDY\",\"rates\":1750},{\"crops\":\"JOWAR\",\"rates\":2430},{\"crops\":\"BAJRA\",\"rates\":1950},{\"crops\":\"MAIZE\",\"rates\":1700},{\"crops\":\"RAGI\",\"rates\":2897},{\"crops\":\"ARHAR(Tur)\",\"rates\":5675},{\"crops\":\"MOONG\",\"rates\":6975},{\"crops\":\"URAD\",\"rates\":5600},{\"crops\":\"COTTON\",\"rates\":5150},{\"crops\":\"GROUNDNUT IN SHELL\",\"rates\":4890},{\"crops\":\"SUNFLOWER SEED\",\"rates\":5388},{\"crops\":\"SOYABEAN\",\"rates\":3399},{\"crops\":\"SESAMUM\",\"rates\":6249},{\"crops\":\"NIGERSEED\",\"rates\":5877},{\"crops\":\"WHEAT\",\"rates\":1840},{\"crops\":\"BARLEY\",\"rates\":1440},{\"crops\":\"GRAM\",\"rates\":4620},{\"crops\":\"MASUR (LENTIL)\",\"rates\":4475},{\"crops\":\"RAPESEED/MUSTARD\",\"rates\":4200},{\"crops\":\"SAFFLOWER\",\"rates\":4945},{\"crops\":\"TORIA\",\"rates\":4190},{\"crops\":\"JUTE\",\"rates\":3700},{\"crops\":\"SUGARCANE $\",\"rates\":275}]}";
void parseFunc(char *data); // forward declaration
void setup() {
char *dataCopy = strdup(json);
parseFunc(dataCopy);
free(dataCopy);
}
void parseFunc(char *mutableData) {
const size_t capacity = JSON_ARRAY_SIZE(23) + JSON_OBJECT_SIZE(1) + 23*JSON_OBJECT_SIZE(2) + 670;
StaticJsonBuffer<capacity> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(mutableData);
JsonArray& rowsArray = root["rows"];
if(!root.success())
{
Particle.publish("parse failed",PRIVATE);
}
else
{
for(JsonObject& result : rowsArray){
JsonObject& rows = result;
Particle.publish("crops", rows["crops"].asString(), 60, PRIVATE); //as<const char*>()
Particle.publish("rates", String(rows["rates"].as<int>()), 60, PRIVATE);
delay(1000);
}
}
}
void loop() {
if (millis() - interval > 5000) {
char *dataCopy = strdup(json);
parseFunc(dataCopy);
free(dataCopy);
interval = millis();
}
}
and results:
This is the spirit we want to encourage
Just get your hands dirty and try - this way you learn the most.
That's why I said this earlier
Spoon-feeding solutions hardly encourages people to try to understand - when they have to put effort into investigation the understanding is a by-product and the acquired knowledge will stick with you much longer and the next problem will take you only a fraction of the time to find the solution.
You probably know the parable about it beeing better to teach someone to fish than just giving him one fish.