Fuzas 0 Posted April 7, 2022 Hello there. Am not expert in API, and JSOn, so hope some can help. I have this JSON file with 20 item, would like to have all the item "names" in a listbox. -> Has only just uload JSON file with 2 item only for examples. "line_items": [ { "id": 20294, "name": "Arduino Uno R3 Chip", "product_id": 257, "variation_id": 0, "quantity": 1, "tax_class": "", "subtotal": "68.00", "subtotal_tax": "17.00", "total": "68.00", "total_tax": "17.00", "taxes": [ { "id": 1, "total": "17", "subtotal": "17" } ], "meta_data": [ { "id": 179468, "key": "_woo_custom_stock_status_email_txt", "value": "På lager", "display_key": "Stock Status", "display_value": "På lager" }, { "id": 179618, "key": "_reduced_stock", "value": "1", "display_key": "_reduced_stock", "display_value": "1" } ], "sku": "3556", "price": 68, "parent_name": null }, { "id": 20295, "name": "Color LED Diode 3mm - Grøn LED", "product_id": 3329, "variation_id": 3332, "quantity": 2, "tax_class": "", "subtotal": "0.80", "subtotal_tax": "0.20", "total": "0.80", "total_tax": "0.20", "taxes": [ { "id": 1, "total": "0.2", "subtotal": "0.2" } ], "meta_data": [ { "id": 179478, "key": "pa_color-led-diode-3mm", "value": "groen-led", "display_key": "Color LED Diode 3mm", "display_value": "Grøn LED" }, { "id": 179479, "key": "_woo_custom_stock_status_email_txt", "value": "På lager", "display_key": "Stock Status", "display_value": "På lager" }, { "id": 179619, "key": "_reduced_stock", "value": "2", "display_key": "_reduced_stock", "display_value": "2" } ] Share this post Link to post
Remy Lebeau 1397 Posted April 7, 2022 You did not show the complete JSON, so the below example is incomplete, but it should give you an idea of what you need: uses ..., System.JSON; ... var Root, Value: TJSONValue; Obj: TJSONObject; Arr: TJSONArray; I: Integer; begin ListBox1.Items.Clear; Root := TJSONObject.ParseJSONValue(...); try Obj := ...; // get a pointer to the parent object first as needed, then... Arr := Obj.GetValue('line_items') as TJSONArray; for Value in Arr do begin Obj := Value as TJSONObject; ListBox1.Items.Add(Obj.Get('name').Value); end; finally Root.Free; end; end; 1 Share this post Link to post
Fuzas 0 Posted April 8, 2022 Then I made it seem like working! : =) Just one last question, how do I get the "value" out of "" id ": 281386" "meta_data": [ { "id": 281179, "key": "_new_order_email_sent", "value": "true" }, { "id": 281385, "key": "carrier", "value": "PostNord (DK)" }, { "id": 281386, "key": "package_number", "value": "123456789" }, { "id": 281387, "key": "tracking_url", "value": "https:\/\/track.shipmondo.com\/pdk\/123456789" }, { "id": 281390, "key": "bewpi_pdf_invoice_sent", "value": "1" } ], Share this post Link to post
Remy Lebeau 1397 Posted April 8, 2022 1 hour ago, Fuzas said: Just one last question, how do I get the "value" out of "" id ": 281386" That is an numeric field. Simply Get() a TJSONValue pointer to it, then type-cast that pointer to TJSONNumber, and then read its AsInt property, eg: Arr := Obj.Get('meta_data') as TJSONArray; for Value in Arr do begin Obj := Value as TJSONObject; id := (Obj.Get('id') as TJSONNumber).AsInt; end; I suggest you read the documentation before going any further. Share this post Link to post
Fuzas 0 Posted April 9, 2022 I have use this but it not working. 😕 var Value: TJSONValue; Obj: TJSONObject; Arr: TJSONArray; id: Integer; begin Arr := Obj.GetValue('meta_data') as TJSONArray; for Value in Arr do begin Obj := Value as TJSONObject; id := (Obj.GetValue('id') as TJSONNumber).AsInt; end; Share this post Link to post
Remy Lebeau 1397 Posted April 11, 2022 On 4/9/2022 at 7:25 AM, Fuzas said: I have use this but it not working. 😕 Well, obviously, because you are not calling TJSONObject.ParseJSONValue() to parse the JSON, and then pointing the Obj variable at the JSON object which holds the 'meta_data' field, You have to do that before calling Obj.GetValue('meta_data'). Once again, just like I stated in my very first reply of this discussion, you have not shown the complete JSON document, so I cannot show you complete Delphi code, only snippets. So it is your job to apply those snippets to your actual JSON document as needed. Share this post Link to post