#include <ArduinoJson.h>
#include <pgmspace.h>
// Inside the brackets, 200 is the capacity of the memory pool in bytes.
// Don't forget to change this value to match your JSON document.
// Use arduinojson.org/v6/assistant to compute the capacity.
StaticJsonDocument<400> doc;
void base_str1(){
Serial.println("------------------------------ string.toCharArray");
String stringOne = "ABcd567890-string";
char Buf[50];
Serial.println(Buf[0]);//空
stringOne.toCharArray(Buf,5);
Serial.println(Buf);
Serial.println(Buf[0]);
Serial.println(Buf[1]);
Serial.println(Buf[6]);//空
Serial.println(Buf[30]);//空
Serial.println(Buf[40]);//空
Serial.println("------------------------------");
}
void base_JSON(){
Serial.println("------------------------------ JSON");
/*
char j1[] ="{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}";
Serial.println("----------------j1");
Serial.println(strlen(j1));
Serial.println(j1);
//char j2[10] 會錯
char j2[100] ="{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}";
Serial.println("----------------j2");
Serial.println(strlen(j2));
Serial.println(j2);
//char j1[]
//char j2[100] 最後長度都一樣
*/
//JSON
//https://arduinojson.org/v6/example/parser/
//char json[]="{\"channel\":{\"id\":2038509,\"name\":\"Home-FishLigh\",\"description\":\"Home-FishLigh\",\"latitude\":\"25.001823\",\"longitude\":\"121.467812\",\"field1\":\"type\",\"created_at\":\"2023-02-20T02:31:01Z\",\"updated_at\":\"2023-02-20T05:37:33Z\",\"last_entry_id\":4},\"feeds\":[{\"created_at\":\"2023-02-20T13:13:04Z\",\"entry_id\":4,\"field1\":\"1\"}]}";
char json[] ="{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}";
Serial.println(json);
// Deserialize the JSON document
DeserializationError error = deserializeJson(doc, json);
if (error) {
Serial.print(F("deserializeJson-> failed: "));
Serial.println(error.f_str());
}
else{
Serial.println("deserializeJson-> OK");
/*
{
"data": [
48.75608,
2.302038
],
"sensor": "gps",
"time": 1351824120
}
*/
// Fetch values - Most of the time, you can rely on the implicit casts.
// In other case, you can do doc["time"].as<long>();
const char* sensor = doc["sensor"];
long time = doc["time"];
double latitude = doc["data"][0];
double longitude = doc["data"][1];
// Print values.
Serial.print("sensor=");
Serial.println(sensor);
Serial.print("time=");
Serial.println(time);
Serial.print("data(1)=");
Serial.println(latitude, 6);
Serial.print("data(2)=");
Serial.println(longitude, 6);
}
}
//https://arduinojson.org/v6/how-to/reduce-memory-usage/
void base_JSON_2(){
Serial.println("------------------------------ JSON(2)");
//JSON ref->https://arduinojson.org/v6/example/parser/
char json[]="{\"channel\":{\"id\":2038509,\"name\":\"Home-FishLigh\",\"description\":\"Home-FishLigh\",\"latitude\":\"25.001823\",\"longitude\":\"121.467812\",\"field1\":\"type\",\"created_at\":\"2023-02-20T02:31:01Z\",\"updated_at\":\"2023-02-20T05:37:33Z\",\"last_entry_id\":4},\"feeds\":[{\"created_at\":\"2023-02-20T13:13:04Z\",\"entry_id\":4,\"field1\":\"1\"}]}";
//char json[] ="{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}";
Serial.println(json);
// Deserialize the JSON document
DeserializationError error = deserializeJson(doc, json);
if (error) {
Serial.print(F("deserializeJson-> failed: "));
Serial.println(error.f_str());
}
else{
Serial.println("deserializeJson-> OK");
// Fetch values - Most of the time, you can rely on the implicit casts.
// In other case, you can do doc["time"].as<long>();
//double latitude = doc["data"][0];
const char* created_at = doc["feeds"][0]["created_at"];
int entry_id= doc["feeds"][0]["entry_id"];
String field1= doc["feeds"][0]["field1"];
//"created_at": "2023-02-20T13:13:04Z",
// "entry_id": 4,
//"field1": "1"
// Print values.
Serial.print("created_at=");Serial.println(created_at);
Serial.print("entry_id=");Serial.println(entry_id);
Serial.print("field1=");Serial.println(field1);
}
}
void setup() {
Serial.begin(115200);
//base_str1();
//base_JSON();
base_JSON_2();
for (;;); //停止
}
void loop() {
}