Arduino JSON 6 - problem z dekodowaniem

User avatar
Duch__
Posts: 2199
Joined: Wed Aug 24, 2016 7:26 pm
Location: Opole
Been thanked: 6 times

Post

Koledzy i koleżanki

Mam problem ze zdekodowaniem takiego przykładowego stringa JSON w ArduinoJson w wersji 6:

Code: Select all

{"1001":{"connected":true,"is_calibrating":false,"shut":20},"1002":{"connected":true,"is_calibrating":false,"shut":30},"1003":{"connected":true,"is_calibrating":false,"shut":50},"1004":{"connected":true,"is_calibrating":false,"shut":100}}
Jest to przykład odebranego formatu JSON z linku bezpośredniego grupy rolet.

Ma ktoś pomysł?
User avatar
pzygmunt
Posts: 20302
Joined: Tue Jan 19, 2016 9:26 am
Location: Paczków
Been thanked: 59 times

Post

Na czym polega problem ?
SUPLA.... Nareszcie w domu.
andrew01
Posts: 163
Joined: Fri May 24, 2019 6:49 am

Post

Sam tego nie używałem ale może będzie pomocne:

https://arduinojson.org/v6/example/
User avatar
Duch__
Posts: 2199
Joined: Wed Aug 24, 2016 7:26 pm
Location: Opole
Been thanked: 6 times

Post

pzygmunt Nie jestem w stanie rozbić odebranego formatu na osobne zmienne.
andrew01 Widziałem te przykłady ale niestety nic nie wskórałem.

W przypadku odczytu transmisji z jednej rolety nie mam żadnego problemu, o tyle w rozbudowanej strukturze jest dramat.
andrew01
Posts: 163
Joined: Fri May 24, 2019 6:49 am

Post

User avatar
Duch__
Posts: 2199
Joined: Wed Aug 24, 2016 7:26 pm
Location: Opole
Been thanked: 6 times

Post

Zauważ że podany przez ciebie link dotyczy dekodowanie w JSON 5, a mnie interesuje JSON 6.

https://arduinojson.org/v6/doc/upgrade/

Składanie jest totalnie inna... :x
wsosniak
Posts: 764
Joined: Sat Jun 02, 2018 8:02 am
Been thanked: 1 time

Post

Duch__ wrote: Sat Jan 04, 2020 10:53 am ..........
W przypadku odczytu transmisji z jednej rolety nie mam żadnego problemu, o tyle w rozbudowanej strukturze jest dramat.
Napisz jak to robsz dla jednej rolety i Ci to chodzi i jak to robisz dla tego ciągu dla wielu rolet , i CI to nie chodzi.
No bo przecież ten Twój string dla wielu rolet to nie jest nic skomplikowanego .
wsosniak
Posts: 764
Joined: Sat Jun 02, 2018 8:02 am
Been thanked: 1 time

Post

Tak na szybko , przykład odczytu procentu zamknięcia rolet , dekodowanie moze wyglądać tak jak poniżej .

Moze to nie jest JSON6 ale w kazdym innym języku programowania jest podobnie. TYm bardziej że operacje na stringach to jedna z najprostszych rzeczy w każdym języku programowania .
You do not have the required permissions to view the files attached to this post.
elmaya
Posts: 1485
Joined: Wed Jun 27, 2018 5:48 pm
Location: El Saucejo - Sevilla

Post

you just have to insert:
["1001"] to ["1004"] before ["connected"], ["is_calibrating"] and ["shut"]

Code: Select all

// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2019
// MIT License
//
// This example shows how to deserialize a JSON document with ArduinoJson.

#include "ArduinoJson.h"

const size_t capacity = 4*JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(4) + 160;
DynamicJsonDocument doc(capacity);

const char* json = "{\"1001\":{\"connected\":true,\"is_calibrating\":false,\"shut\":20},\"1002\":{\"connected\":true,\"is_calibrating\":false,\"shut\":30},\"1003\":{\"connected\":true,\"is_calibrating\":false,\"shut\":50},\"1004\":{\"connected\":true,\"is_calibrating\":false,\"shut\":100}}";

void setup() {

  Serial.begin(115200);
  delay(1000);
  Serial.println("");
  
    // Deserialize the JSON document
  DeserializationError error = deserializeJson(doc, json);

  // Fetch values.

  bool conectted_1001 = doc["1001"]["connected"];
  bool is_calibrating_1001 = doc["1001"]["is_calibrating"];
  int shut_1001 = doc["1001"]["shut"];
  
  bool conectted_1002 = doc["1002"]["connected"];
  bool is_calibrating_1002 = doc["1002"]["is_calibrating"];
  int shut_1002 = doc["1002"]["shut"];
  
  bool conectted_1003 = doc["1003"]["connected"];
  bool is_calibrating_1003 = doc["1003"]["is_calibrating"];
  int shut_1003 = doc["1003"]["shut"];
  
  bool conectted_1004 = doc["1004"]["connected"];
  bool is_calibrating_1004 = doc["1004"]["is_calibrating"];
  int shut_1004 = doc["1004"]["shut"];

  // Print values.
  Serial.print("1001");Serial.print(conectted_1001);Serial.print(is_calibrating_1001);Serial.println(shut_1001);
  Serial.print("1002");Serial.print(conectted_1002);Serial.print(is_calibrating_1002);Serial.println(shut_1002);
  Serial.print("1003");Serial.print(conectted_1003);Serial.print(is_calibrating_1003);Serial.println(shut_1003);
  Serial.print("1004");Serial.print(conectted_1004);Serial.print(is_calibrating_1004);Serial.println(shut_1004);

 }

void loop() {
  // put your main code here, to run repeatedly:

}
User avatar
Duch__
Posts: 2199
Joined: Wed Aug 24, 2016 7:26 pm
Location: Opole
Been thanked: 6 times

Post

This is exactly what i need!!! Thanks!

Return to “Pomoc”