Page 3 of 3 FirstFirst 1 2 3
Results 31 to 41 of 41

Thread: Json uitlezen en fatsoendelijk in een listbox of een memo

  1. #31
    ow ja klopt moet response staan inplaats van type ik heb wat gestoeid er mee maar als er response staat krijg ik deze foutmelding
    Name:  Image2.jpg
Views: 194
Size:  6.0 KB

  2. #32
    Zonder code kunnen we natuurlijk niet zien wat er mis gaat.

    Maar om even bij de code van de eerste pagina te blijven is hier de oplossing om de json weer naar een stringgrid te zetten.

    Ditmaal bevat de response dus geen array met de naam switches maar is de response zelf een array.

    De testdata2.json:
    Json Code:
    1. {
    2.     "status": "ok",
    3.     "version": "3.38",
    4.     "request": {
    5.         "route": "/el"
    6.     },
    7.     "response": [{
    8.         "type": "electricity",
    9.         "tariff": 1,
    10.         "consumed": 5505.239,
    11.         "produced": 0.001
    12.     }, {
    13.         "type": "electricity",
    14.         "tariff ": 2,
    15.         "consumed": 5563.550,
    16.         "produced": 0.001
    17.     }, {
    18.         "type": "gas",
    19.         "consumed": 5564.793,
    20.         "timestamp": 1498086000
    21.     }]
    22. }

    Delphi Code:
    1. uses System.JSON;
    2.  
    3. function LoadFileToStr(const FileName: TFileName): String;
    4. var
    5.   FileStream: TFileStream;
    6.   Bytes: TBytes;
    7. begin
    8.   Result := '';
    9.   FileStream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
    10.   try
    11.     if FileStream.Size > 0 then
    12.     begin
    13.       SetLength(Bytes, FileStream.Size);
    14.       FileStream.Read(Bytes[0], FileStream.Size);
    15.     end;
    16.     Result := TEncoding.ASCII.GetString(Bytes);
    17.   finally
    18.       FileStream.Free;
    19.   end;
    20. end;
    21.  
    22. procedure AutoSizeCol(Grid: TStringGrid; Column: integer);
    23. var
    24.   i, W, WMax: integer;
    25. begin
    26.   WMax := 0;
    27.   for i := 0 to Grid.RowCount - 1 do begin
    28.     W := Grid.Canvas.TextWidth(Grid.Cells[Column, i]);
    29.     if W > WMax then
    30.       WMax := W;
    31.   end;
    32.   Grid.ColWidths[Column] := WMax + 15;
    33. end;
    34.  
    35. procedure TForm9.Button1Click(Sender: TObject);
    36. var
    37.   JsonString: String;
    38.   jsv: TJsonValue;
    39.   jsResponse: TJsonPair;
    40.   jsSwitches: TJsonPair;
    41.   jsArr: TJsonArray;
    42.   jso: TJsonObject;
    43.   jsPair: TJsonPair;
    44.   i, j: integer;
    45. begin
    46.   JsonString := LoadFileToStr('c:\temp\testdata2.json');
    47.   jsv := TJsonObject.ParseJSONValue(JsonString);
    48.   try
    49.     if jsv = nil then raise Exception.Create('error in json');
    50.  
    51.     jsResponse := (jsv as TJsonObject).Get('response');
    52.     if jsResponse = nil then raise Exception.Create('no response in json');
    53.  
    54.     // in dit geval is response de array dus moeten we jsResponse naar een TJsonArray casten
    55.     jsArr := jsResponse.JsonValue as TJsonArray;
    56.  
    57.     StringGrid1.RowCount := jsArr.Count + 1;
    58.     StringGrid1.ColCount := 1;
    59.     for i := 0 to jsArr.Count - 1 do
    60.     begin
    61.       jso := jsArr.Items[i] as TJsonObject;
    62.       for jsPair in jso do
    63.       begin
    64.         j := StringGrid1.Rows[0].IndexOf(jsPair.JsonString.Value);
    65.         if j < 0 then
    66.         begin // kolom toevoegen
    67.           if StringGrid1.Cells[StringGrid1.ColCount - 1, 0] <> '' then
    68.             StringGrid1.ColCount := StringGrid1.ColCount + 1;
    69.           j := StringGrid1.ColCount - 1;
    70.           StringGrid1.Cells[j, 0] := jsPair.JsonString.Value;
    71.         end;
    72.         StringGrid1.Cells[j, i + 1] := jsPair.JsonValue.Value;
    73.       end;
    74.     end;
    75.  
    76.     for i := 0 to StringGrid1.ColCount - 1 do AutoSizeCol(StringGrid1, i);
    77.  
    78.   finally
    79.     jsv.Free();
    80.   end;
    81. end;

    Resultaat:
    Name:  2017-06-21 23_08_11-Form9.png
Views: 188
Size:  6.2 KB

  3. #33
    Harstikke bedankt ik heb de code van mij bewerkt en het werkt nu maar nu zit ik weer met een ander probleem:

    ik wil de energy link uit lezen maar hij pakt niet alles waarom moet dat json zo omslachtig allemaal maar dat zal wel aan mij liggen

    json: hij leest dit uit:

    {"status": "ok", "version": "3.38", "request": {"route": "/get-status" }, "response": {"preset":0,"time":"2017-06-23 22:14","switches":[{"id":0,"type":"switch","status":"on"},{"id":1,"ty pe":"switch","status":"off"},{"id":2,"type":"dimme r","status":"off","dimlevel":0},{"id":3,"type":"sw itch","status":"on"},{"id":4,"type":"switch","stat us":"off"},{"id":5,"type":"switch","status":"on"}, {"id":6,"type":"switch","status":"on"},{"id":7,"ty pe":"dimmer","status":"on","dimlevel":100},{"id":8 ,"type":"switch","status":"off"},{"id":9,"type":"s witch","status":"on"},{"id":10,"type":"switch","st atus":"on"},{"id":11,"type":"dimmer","status":"off ","dimlevel":0},{"id":15,"type":"switch","status": "on"},{"id":16,"type":"virtual"},{"id":17,"type":" virtual"},{"id":18,"type":"switch","status":"off"} ,{"id":19,"type":"dimmer","status":"off","dimlevel ":0},{"id":20,"type":"switch","status":"on"},{"id" :21,"type":"switch","status":"off"},{"id":22,"type ":"switch","status":"off"}],"uvmeters":[],"windmeters":[],"rainmeters":[],"thermometers":[{"id":0,"te":25.5,"hu":46,"favorite":"no"}],"weatherdisplays":[], "energymeters": [], "energylinks": [{"id":0,"tariff":1,"s1":{"po":0,"dayTotal":592.00, "po+":14,"po+t":"10:46","po-":0,"po-t":"00:01"},"s2":null,"aggregate":{"po":570,"dayTo tal":11.47,"po+":2560,"po+t":"06:58","po-":200,"po-t":"02:01"},"used":{"po":570,"dayTotal":11.38,"po+ ":2560,"po+t":"06:58","po-":200,"po-t":"02:01"},"gas":{"lastHour":0.00,"dayTotal":0.68 },"kwhindex":0.00}], "heatlinks": [{"id": 0, "pump": "off", "heating": "off", "dhw": "off", "rte": 25.500, "rsp": 5.000, "tte": 5.000, "ttm": null, "wp": 1.597, "wte": 43.000, "ofc": 0, "odc": 0}], "kakusensors": [{"id":0,"status":"no","timestamp":"17:56"},{"id":1 ,"status":"no","timestamp":"21:42"},{"id":2,"statu s":null,"timestamp":"00:00"},{"id":3,"status":"no" ,"timestamp":"16:31"},{"id":4,"status":"no","times tamp":"22:11"},{"id":5,"status":"yes","timestamp": "22:11"},{"id":6,"status":null,"timestamp":"00:00" },{"id":7,"status":null,"timestamp":"00:00"},{"id" :8,"status":null,"timestamp":"00:00"},{"id":9,"sta tus":"yes","timestamp":"05:45"}]}}
    het rode gedeelte leest hij maar half uit dus niet de waarde
    Click image for larger version. 

Name:	Image2.jpg 
Views:	80 
Size:	10.9 KB 
ID:	7587

  4. #34
    energylinks is ook weer een json-array.

    Maar zonder code kunnen we natuurlijk niet zeggen wat er bij jou verkeerd gaat.

  5. #35
    Quote Originally Posted by rvk View Post
    energylinks is ook weer een json-array.

    Maar zonder code kunnen we natuurlijk niet zeggen wat er bij jou verkeerd gaat.
    code:

    Delphi Code:
    1. Var
    2.   JsonString: String;
    3.   jsv: TJsonValue;
    4.   jsResponse: TJsonPair;
    5.   jsSwitches: TJsonPair;
    6.   jstype: TJsonPair;
    7.   jste: TJsonPair;
    8.   jsArr: TJsonArray;
    9.   jso: TJsonObject;
    10.   jsPair: TJsonPair;
    11.   i, j: integer;
    12.  ms:TMemoryStream;
    13. begin
    14.  
    15.  
    16.   Energygrid2.ClearColumns;
    17.   JsonString := loadfiletostr('status.json');
    18.   jsv := TJsonObject.ParseJSONValue(JsonString);
    19.   try
    20.     jsResponse := (jsv as TJsonObject).Get('response');
    21.     jsSwitches := (jsResponse.JsonValue as TJsonObject).Get('energylinks');
    22.    
    23.  
    24.     jsArr := jsSwitches.JsonValue as TJsonArray;
    25.  
    26.     Energygrid2.RowCount := jsArr.Count;
    27.    
    28.     while Energygrid2.ColumnCount > 1 do Energygrid2.Columns[0].DisposeOf;
    29.     for i := 0 to jsArr.Count - 1 do
    30.     begin
    31.       jso := jsArr.Items[i] as TJsonObject;
    32.       for jsPair in jso do
    33.       begin
    34.         j := 0;
    35.         while (j < Energygrid2.ColumnCount) and (Energygrid2.Columns[j].Header <> jsPair.JsonString.Value) do
    36.           Inc(j);
    37.         if (j >= Energygrid2.ColumnCount) then
    38.         begin
    39.           Energygrid2.AddObject(TStringColumn.Create(Self));
    40.           Energygrid2.Columns[j].Header := jsPair.JsonString.Value;
    41.         end;
    42.         Energygrid2.Cells[j, i] := jsPair.JsonValue.Value;
    Last edited by GolezTrol; 25-Jun-17 at 01:50.

  6. #36
    En wat is dan het probleem. Ik zie in jouw screenshot wel de gegevens staan die jij rood gemarkeerd had.

    Ik zie id=0, tariff=1, s1=leeg omdat het multiple values heeft, s2=null

    Dus wat is het probleem?
    Als jij de waardes in s1 wilt weergeven dan zul je die ook weer "af moeten lopen". s1 is geen enkele key met een waarde maar bevat ook weer meerdere waardes. Idem met aggregate, used en gas.

    Dus je moet iets in gaan bouwen van:
    Delphi Code:
    1. if jsPair.JsonString.Value = 's1' then
    2. begin
    3.   // en hier weer een loop maken die de jsPair als TJsonObject cast naar jsPair_s1
    4.   // en met "for jsPair_s1 in jso_s1 do" alle waardes doorloopt.
    5. end;

    Hieronder weer even de json mooi neergezet zodat het wat duidelijker is:
    JSON Code:
    1. {
    2.   "status": "ok",
    3.   "version": "3.38",
    4.   "request": {
    5.     "route": "/get-status"
    6.   },
    7.   "response": {
    8.     "preset": 0,
    9.     "time": "2017-06-23 22:14",
    10.     "switches": [
    11.       {
    12.         "id": 0,
    13.         "type": "switch",
    14.         "status": "on"
    15.       },
    16.       {
    17.         "id": 1,
    18.         "ty pe": "switch",
    19.         "status": "off"
    20.       },
    21.       {
    22.         "id": 2,
    23.         "type": "dimme r",
    24.         "status": "off",
    25.         "dimlevel": 0
    26.       },
    27.       {
    28.         "id": 3,
    29.         "type": "sw itch",
    30.         "status": "on"
    31.       },
    32.       {
    33.         "id": 4,
    34.         "type": "switch",
    35.         "stat us": "off"
    36.       },
    37.       {
    38.         "id": 5,
    39.         "type": "switch",
    40.         "status": "on"
    41.       },
    42.       {
    43.         "id": 6,
    44.         "type": "switch",
    45.         "status": "on"
    46.       },
    47.       {
    48.         "id": 7,
    49.         "ty pe": "dimmer",
    50.         "status": "on",
    51.         "dimlevel": 100
    52.       },
    53.       {
    54.         "id": 8,
    55.         "type": "switch",
    56.         "status": "off"
    57.       },
    58.       {
    59.         "id": 9,
    60.         "type": "s witch",
    61.         "status": "on"
    62.       },
    63.       {
    64.         "id": 10,
    65.         "type": "switch",
    66.         "st atus": "on"
    67.       },
    68.       {
    69.         "id": 11,
    70.         "type": "dimmer",
    71.         "status": "off ",
    72.         "dimlevel": 0
    73.       },
    74.       {
    75.         "id": 15,
    76.         "type": "switch",
    77.         "status": "on"
    78.       },
    79.       {
    80.         "id": 16,
    81.         "type": "virtual"
    82.       },
    83.       {
    84.         "id": 17,
    85.         "type": " virtual"
    86.       },
    87.       {
    88.         "id": 18,
    89.         "type": "switch",
    90.         "status": "off"
    91.       },
    92.       {
    93.         "id": 19,
    94.         "type": "dimmer",
    95.         "status": "off",
    96.         "dimlevel ": 0
    97.       },
    98.       {
    99.         "id": 20,
    100.         "type": "switch",
    101.         "status": "on"
    102.       },
    103.       {
    104.         "id": 21,
    105.         "type": "switch",
    106.         "status": "off"
    107.       },
    108.       {
    109.         "id": 22,
    110.         "type ": "switch",
    111.         "status": "off"
    112.       }
    113.     ],
    114.     "uvmeters": [],
    115.     "windmeters": [],
    116.     "rainmeters": [],
    117.     "thermometers": [
    118.       {
    119.         "id": 0,
    120.         "te": 25.5,
    121.         "hu": 46,
    122.         "favorite": "no"
    123.       }
    124.     ],
    125.     "weatherdisplays": [],
    126.     "energymeters": [],
    127.     "energylinks": [
    128.       {
    129.         "id": 0,
    130.         "tariff": 1,
    131.         "s1": {
    132.           "po": 0,
    133.           "dayTotal": 592,
    134.           "po+": 14,
    135.           "po+t": "10:46",
    136.           "po-": 0,
    137.           "po-t": "00:01"
    138.         },
    139.         "s2": null,
    140.         "aggregate": {
    141.           "po": 570,
    142.           "dayTo tal": 11.47,
    143.           "po+": 2560,
    144.           "po+t": "06:58",
    145.           "po-": 200,
    146.           "po-t": "02:01"
    147.         },
    148.         "used": {
    149.           "po": 570,
    150.           "dayTotal": 11.38,
    151.           "po+ ": 2560,
    152.           "po+t": "06:58",
    153.           "po-": 200,
    154.           "po-t": "02:01"
    155.         },
    156.         "gas": {
    157.           "lastHour": 0,
    158.           "dayTotal": 0.68
    159.         },
    160.         "kwhindex": 0
    161.       }
    162.     ],
    163.     "heatlinks": [
    164.       {
    165.         "id": 0,
    166.         "pump": "off",
    167.         "heating": "off",
    168.         "dhw": "off",
    169.         "rte": 25.5,
    170.         "rsp": 5,
    171.         "tte": 5,
    172.         "ttm": null,
    173.         "wp": 1.597,
    174.         "wte": 43,
    175.         "ofc": 0,
    176.         "odc": 0
    177.       }
    178.     ],
    179.     "kakusensors": [
    180.       {
    181.         "id": 0,
    182.         "status": "no",
    183.         "timestamp": "17:56"
    184.       },
    185.       {
    186.         "id": 1,
    187.         "status": "no",
    188.         "timestamp": "21:42"
    189.       },
    190.       {
    191.         "id": 2,
    192.         "statu s": null,
    193.         "timestamp": "00:00"
    194.       },
    195.       {
    196.         "id": 3,
    197.         "status": "no",
    198.         "timestamp": "16:31"
    199.       },
    200.       {
    201.         "id": 4,
    202.         "status": "no",
    203.         "times tamp": "22:11"
    204.       },
    205.       {
    206.         "id": 5,
    207.         "status": "yes",
    208.         "timestamp": "22:11"
    209.       },
    210.       {
    211.         "id": 6,
    212.         "status": null,
    213.         "timestamp": "00:00"
    214.       },
    215.       {
    216.         "id": 7,
    217.         "status": null,
    218.         "timestamp": "00:00"
    219.       },
    220.       {
    221.         "id": 8,
    222.         "status": null,
    223.         "timestamp": "00:00"
    224.       },
    225.       {
    226.         "id": 9,
    227.         "sta tus": "yes",
    228.         "timestamp": "05:45"
    229.       }
    230.     ]
    231.   }
    232. }

  7. #37
    oké,

    en hebt u daar een voorbeeld van?
    Last edited by GolezTrol; 25-Jun-17 at 01:51.

  8. #38
    Quote Originally Posted by kjmjans View Post
    oké,
    en hebt u daar een voorbeeld van?
    Bedoel je of ik de complete code daarvoor wil schrijven?

    Ik gaf al aan hoe het ongeveer moet.
    delphi Code:
    1. if jsPair.JsonString.Value = 's1' then
    2. begin
    3.   // en hier weer een loop maken die de jsPair als TJsonObject cast naar jsPair_s1
    4.   // en met "for jsPair_s1 in jso_s1 do" alle waardes doorloopt.
    5. end;

    Verder zou dan ook de vraag zijn welke waarde uit s1 en e.v. s2 je zou willen hebben (of allemaal). Maar met alle waarde hem je weer kans dat het niet in de breedte van je stringgrid/beeldscherm past.

  9. #39
    Ik zat weer even achter de computer en zal je laten zien hoe je subkeys uit zou moeten lezen.

    Je ziet dat s1, aggregate e.d. ook weer subkeys heeft. Dus je moet controleren binnen je loop of je te maken hebt met één enkele waarde of met een key met multiple waarden. Dit doe je door te controleren of jsPair.JsonValue een TJSONObject is.

    Hier heb ik code die een memo vult.
    (zie de code "hier wordt het dus interessant" wat ik bedoel)

    Delphi Code:
    1. procedure TForm1.Button1Click(Sender: TObject);
    2. var
    3.   JsonString: String;
    4.   jsv: TJsonValue;
    5.   jsResponse: TJsonPair;
    6.   jsEnergyLinks: TJsonPair;
    7.   jsArr: TJsonArray;
    8.   jso: TJsonObject;
    9.   jsPair: TJsonPair;
    10.   jso2: TJsonObject;
    11.   jsPair2: TJsonPair;
    12.   i: integer;
    13. begin
    14.   JsonString := LoadFileToStr('c:\temp\testdata3.json');
    15.   jsv := TJsonObject.ParseJSONValue(JsonString);
    16.   try
    17.     if jsv = nil then raise Exception.Create('error in json');
    18.  
    19.     jsResponse := (jsv as TJsonObject).Get('response');
    20.     if jsResponse = nil then raise Exception.Create('no response in json');
    21.  
    22.     jsEnergyLinks := (jsResponse.JsonValue as TJsonObject).Get('energylinks');
    23.     if jsEnergyLinks = nil then raise Exception.Create('no energylinks in json');
    24.  
    25.     // in dit geval is response de array dus moeten we jsResponse naar een TJsonArray casten
    26.     jsArr := jsEnergyLinks.JsonValue as TJsonArray;
    27.  
    28.     for i := 0 to jsArr.Count - 1 do // in geval van energylinks is dit er maar 1
    29.     begin
    30.       jso := jsArr.Items[i] as TJsonObject;
    31.       for jsPair in jso do
    32.       begin
    33.  
    34.         Memo1.Lines.Add(jsPair.JsonString.Value + ' = ' + jsPair.JsonValue.Value);
    35.  
    36.         // ===================
    37.         // hier wordt het dus interessant
    38.         // ===================
    39.         if jsPair.JsonValue is TJSONObject then // is het een object met meerdere keys ?
    40.         begin
    41.           jso2 := jsPair.JsonValue as TJsonObject; // loop dan ook al die keys weer door
    42.           for jsPair2 in jso2 do
    43.           begin
    44.             Memo1.Lines.Add(jsPair.JsonString.Value + '.' + jsPair2.JsonString.Value + ' = ' + jsPair2.JsonValue.Value);
    45.           end;
    46.         end;
    47.         // ===================
    48.  
    49.  
    50.       end;
    51.     end;
    52.  
    53.   finally
    54.     jsv.Free();
    55.   end;
    56. end;

    Geeft dit als resultaat.
    Code:
    id = 0
    tariff = 1
    s1 = 
    s1.po = 0
    s1.dayTotal = 592
    s1.po+ = 14
    s1.po+t = 10:46
    s1.po- = 0
    s1.po-t = 00:01
    s2 = null
    aggregate = 
    aggregate.po = 570
    aggregate.dayTo tal = 11,47
    aggregate.po+ = 2560
    aggregate.po+t = 06:58
    aggregate.po- = 200
    aggregate.po-t = 02:01
    used = 
    used.po = 570
    used.dayTotal = 11,38
    used.po+  = 2560
    used.po+t = 06:58
    used.po- = 200
    used.po-t = 02:01
    gas = 
    gas.lastHour = 0
    gas.dayTotal = 0,68
    kwhindex = 0
    Ik neem aan dat je dit zelf wel weer in een stringgrid of andere vorm kunt gieten.

  10. #40
    Harstikke bedankt ik ga eens klooien met de code als ik er wat van kan maken. heb het de vorige keer ook gedaan toen is het mij gelukt

    nogmaals harstikke bedankt.
    Last edited by GolezTrol; 25-Jun-17 at 01:51.

  11. #41
    Je hoeft niet het hele bericht te citeren als je antwoord geeft, zeker niet met die lappen code van rvk.
    1+1=b

Page 3 of 3 FirstFirst 1 2 3

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •