개발자/Arduino

ArduinoJson 데이터의 Decoding / Ecoding 예제

지구빵집 2020. 10. 8. 16:18
반응형

 

 

간단하게 아두이노 Nano 33 IoT 보드에서 확인할 수 있도록 예제를 제공합니다.

 

Decoding / Parsing 예제 구문은 아래에 있고 IoT보드에 올리려서 테스트하느 코드도 함께 올립니다.

 

char json[] = "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}";

StaticJsonBuffer<200> jsonBuffer;

JsonObject& root = jsonBuffer.parseObject(json);

const char* sensor = root["sensor"];
long time          = root["time"];
double latitude    = root["data"][0];
double longitude   = root["data"][1];

 

#include <ArduinoJson.h>

StaticJsonBuffer<200> jsonBuffer;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

}

void loop() {
  StaticJsonBuffer<200> jsonBuffer;
  JsonObject& object = jsonBuffer.createObject();
  object["hello"] = "world";
  object.printTo(Serial);

  delay(5000);

}

 

Encoding / Generating 예제 구문은 아래에 있고 IoT보드에 올리려서 테스트하느 코드도 함께 올립니다.  

 

StaticJsonBuffer<200> jsonBuffer;

JsonObject& root = jsonBuffer.createObject();
root["sensor"] = "gps";
root["time"] = 1351824120;

JsonArray& data = root.createNestedArray("data");
data.add(48.756080, 6);  // 6 is the number of decimals to print
data.add(2.302038, 6);   // if not specified, 2 digits are printed

root.printTo(Serial);
// This prints:
// {"sensor":"gps","time":1351824120,"data":[48.756080,2.302038]}

 

#include <ArduinoJson.h>

StaticJsonBuffer<200> jsonBuffer;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

}

void loop() {
JsonObject& root = jsonBuffer.createObject();
root["sensor"] = "gps";
root["time"] = 1351824120;
 
JsonArray& data = root.createNestedArray("data");
data.add(48.756080, 6);  // 6 is the number of decimals to print
data.add(2.302038, 6);   // if not specified, 2 digits are printed
 
root.printTo(Serial);

Serial.println("");

delay(5000);
}

 

 

https://arduinojson.org/ 

 

 

 

 

 

반응형