[vz-users] JSON parsen

Christian Wulff christianwulff at gmx.de
Sun Jul 30 14:28:44 CEST 2017


Moin Frank,

 

nee, den Hinweis brauche ich nicht. Das weiss ich selber.

Ich habe mir von beiden JSONs die Parsing programs von dem Assistant ausgedruckt und nebeneinandergelegt und mit dem funktionierenden Arduino Programm vom ersten JSON verglichen.

Was soll ich sagen?! Ich kann das nicht nachvollziehen.

 

Das erste JSON hat nur JSON Objects. Ich denke das verstehe ich. Jedenfalls funktioniert mein Programm damit.

Das zweite JSON hat aber JSON Arrays. Damit komme ich nicht klar.

 

Das mag aber vielleicht daran liegen, das mein original Beispiel Parsing Program von der Library völlig anders aussieht, als das vom Assistant?!

 

So sieht mein Parsing Program aus:

 

strcpy(userData->color, root["entity"]["color"]);

 

for (int pos=0; pos<7; pos++){ 

strcpy(resChannel,channelfolder);

strcat(resChannel,UUID[pos]);

strcat(resChannel,channelextension);

if (connect(server)) {

  if (sendRequest(server, resChannel) && skipResponseHeaders()) {

    UserData userData;

      if (readReponseChannel(&userData)) {

        textcol[pos]=rgb565(userData.color);

      }

    }

  }

  disconnect();

}

 

So sieht das vom Assistant aus:

 

const size_t bufferSize = JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(9) + 190;

DynamicJsonBuffer jsonBuffer(bufferSize);

 

const char* json = "{\"version\":\"0.3\",\"entity\":{\"uuid\":\"05ce3532-97c7-1356-a454-754d9534563c\",\"type\":\"temperature\",\"active\":false,\"color\":\"#009933\",\"fillstyle\":0,\"public\":true,\"style\":\"lines\",\"title\":\"T01 Aussen\",\"yaxis\":\"auto\"}}";

 

JsonObject& root = jsonBuffer.parseObject(json);

 

const char* version = root["version"]; // "0.3"

 

JsonObject& entity = root["entity"];

const char* entity_uuid = entity["uuid"]; // "05ce3532-97c7-1356-a454-754d9534563c"

const char* entity_type = entity["type"]; // "temperature"

bool entity_active = entity["active"]; // false

const char* entity_color = entity["color"]; // "#009933"

int entity_fillstyle = entity["fillstyle"]; // 0

bool entity_public = entity["public"]; // true

const char* entity_style = entity["style"]; // "lines"

const char* entity_title = entity["title"]; // "T01 Aussen"

const char* entity_yaxis = entity["yaxis"]; // "auto"

 

 

 

…..da versteh ich leider nur Bahnhof. Da ist ja nichts vergleichbar/nachvollziehbar

 

Deswegen kann ich für das zwei JSON die Agfrage von einem JSON Object nicht auf ein JSON Array umbauen, weil ich nicht weiss, wie ich die Einträge addressiere?!

 

Lieben Gruß,

Chris

 

 

 

 

 

Von: Frank Richter [mailto:frank.richter83 at gmail.com] 
Gesendet: Sonntag, 30. Juli 2017 02:19
An: volkszaehler.org - users
Betreff: Re: [vz-users] JSON parsen

 

Moin Christian,

 

brauchst du echt nochmal einen Hinweis auf https://bblanchon.github.io/ArduinoJson/assistant/ ?

 

Wenn du dort das fragliche JSON reinkopierst, komm folgendes raus:

 

const size_t bufferSize = JSON_ARRAY_SIZE(1) + 2*JSON_ARRAY_SIZE(2) + JSON_ARRAY_SIZE(3) + JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(8) + 220;
DynamicJsonBuffer jsonBuffer(bufferSize);

const char* json = "{\"version\":\"0.3\",\"data\":{\"tuples\":[[1501258250761,20.312,1]],\"uuid\":\"05ce3700-97c7-11e6-acf4-754d9577033c\",\"from\":1501258204011,\"to\":1501258250761,\"min\":[1501258250761,20.312],\"max\":[1501258250761,20.312],\"average\":20.312,\"rows\":2}}";

JsonObject& root = jsonBuffer.parseObject(json);

const char* version = root["version"]; // "0.3"

JsonObject& data = root["data"];

JsonArray& data_tuples0 = data["tuples"][0];
long data_tuples00 = data_tuples0[0]; // 1501258250761
float data_tuples01 = data_tuples0[1]; // 20.312
int data_tuples02 = data_tuples0[2]; // 1

const char* data_uuid = data["uuid"]; // "05ce3700-97c7-11e6-acf4-754d9577033c"
long data_from = data["from"]; // 1501258204011
long data_to = data["to"]; // 1501258250761

long data_min0 = data["min"][0]; // 1501258250761
float data_min1 = data["min"][1]; // 20.312

long data_max0 = data["max"][0]; // 1501258250761
float data_max1 = data["max"][1]; // 20.312

float data_average = data["average"]; // 20.312
int data_rows = data["rows"]; // 2

 

Was ist denn da noch unklar?

 

Gruß

Frank

 

Am 30.07.2017 01:39 schrieb "Christian Wulff" <christianwulff at gmx.de>:

Moin,

 

Ich komme leider beim JSON parsen nicht weiter.

 

Das JSON des Channels habe ich inzwischen gut genug unter Kontrolle:

{

  "version": "0.3",

   "entity": {

     "uuid": "05ce3532-97c7-1356-a454-754d9534563c",

     "type": "temperature",

     "active": false,

     "color": "#009933",

     "fillstyle": 0,

     "public": true,

     "style": "lines",

     "title": "T01 Aussen",

     "yaxis": "auto"

   }

}

Dies hat die Expression:

JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(9)

 

Da lese ich die "color" aus.

Die Adresse ist root["entity"]["color"]

 

Soweit kann ich das nachvollziehen.

 

Nun habe ich das Data JSON:

{

   "version":"0.3",

   "data":{

      "tuples":[

         [1501258250761,20.312,1]],

      "uuid":"05ce3700-97c7-11e6-acf4-754d9577033c",

      "from":1501258204011,

      "to":1501258250761,

      "min":[1501258250761,20.312],

      "max":[1501258250761,20.312],

      "average":20.312,

      "rows":2

      }

}

Dies hat die Expression:

JSON_ARRAY_SIZE(1) + 2*JSON_ARRAY_SIZE(2) + JSON_ARRAY_SIZE(3) + JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(8)

 

Da möchte ich aus dem Tuple in der 4. Zeile "1501258250761" (das ist der Timestamp) und "20.312" (das ist die Temperatur) auslesen.

Wie lauten denn davon die Adressen?

root["data"]["tuples ....... und da verließen sie mich?!

 

Könnte mir da jemand weiterhelfen?

 

Danke und lieben Gruß,

Chris

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://demo.volkszaehler.org/pipermail/volkszaehler-users/attachments/20170730/6e73a4e4/attachment-0001.html>


More information about the volkszaehler-users mailing list