ArduinoJson Lib

hey guys!

I am quite new to particle.

I am trying ArduinoJson library.

My goal is to sent in a function a JSON String so i can parse and the arguments in my code as variables. i dont know why but i am losing my data somewhere.

i spent days in this and i am stuck. I saw lots of people with the same problem but i didnt get a working solution to my case.

can anyone please give me a hint?

thanks in advance,

pedro

#include <ArduinoJson.h>

void setup() {
    Serial.begin(9600);
    //this will trigger the function to make the http request to receive the json 
    Particle.function("JSON", getJSON);
}

void loop() {
  // not used in this example
}

int getJSON(String JsonFile) {
          
    StaticJsonDocument<350> doc;
    
    char jsonString[JsonFile.length() + 1];
    JsonFile.toCharArray(jsonString, sizeof(jsonString));
    
    // Deserialize the JSON document
    DeserializationError error = deserializeJson(doc, jsonString);
    
    // Test if parsing succeeds.
    if (error) {
        Particle.publish("event", "ERRO");
        return -1;
    }
    
    const char* sensorChar = doc["sensor"];
    
    Serial.println(sensorChar);

    // Publish the value of the "sensor" key as a String object
    Particle.publish("event", sensorChar);
    
    return 1;
}

I recommend not using that library and using the JSONWriter class, which is built into Device OS, for generating JSON, instead.

Yes, I saw the publish and assumed it was composing JSON. For parsing JSON there’s the built-in JSONValue.

Another option for parsing JSON is JsonParserGeneratorRK.

Also, if you’re making a HTTP request to a public server (not on your local LAN), then it’s usually better to use a Webhook and mustache instead.

2 Likes

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.