I seem to be having a little trouble parsing JSON, but not ALL JSON. I'm pulling data from Shopify, and when I GET products.json it parses correctly. When I get a specific product, I get an 'Invalid Class Typecast' error.
Using the dbxjson.pas in Delphi XE4, and I am just learning how to use it. Some of the code below was taken from an answer to a previous question about using the JSON parser in general.
I'm simply putting the JSON into a memo field, then calling parsejson2(memo1.text), and it puts the results in memo2.
I am correctly GETting the JSON, and have verified that it is correct through json.parser.online.fr.
Here is the JSON output that works with the code I have:
{"products":[{"id":11332270357,"title":"Burton Custom Freestyle 151","body_html":"\u003cstrong\u003eGood snowboard!\u003c\/strong\u003e","vendor":"Burton","product_type":"Snowboard","created_at":"2017-07-26T12:55:21-04:00","handle":"burton-custom-freestyle-151","updated_at":"2017-07-26T12:55:21-04:00","published_at":"2017-07-26T12:55:21-04:00","template_suffix":null,"published_scope":"global","tags":"\"Big Air\", Barnes \u0026 Noble, John's Fav","variants":[{"id":48188219989,"product_id":11332270357,"title":"Default Title","price":"0.00","sku":"","position":1,"grams":0,"inventory_policy":"deny","compare_at_price":null,"fulfillment_service":"manual","inventory_management":null,"option1":"Default Title","option2":null,"option3":null,"created_at":"2017-07-26T12:55:21-04:00","updated_at":"2017-07-26T12:55:21-04:00","taxable":true,"barcode":null,"image_id":null,"inventory_quantity":1,"weight":0.0,"weight_unit":"lb","old_inventory_quantity":1,"requires_shipping":true}],"options":[{"id":13730250389,"product_id":11332270357,"name":"Title","position":1,"values":["Default Title"]}],"images":[],"image":null},{"id":11400089237,"title":"Pile of Rocks","body_html":"Wicked cool pile of rocks from Hawaii!","vendor":"test_titan","product_type":"","created_at":"2017-08-01T09:44:11-04:00","handle":"pile-of-rocks","updated_at":"2017-08-01T09:44:54-04:00","published_at":"2017-08-01T09:40:55-04:00","template_suffix":null,"published_scope":"global","tags":"","variants":[{"id":48653472149,"product_id":11400089237,"title":"pile \/ various","price":"1000.00","sku":"12345678974346547000","position":1,"grams":14,"inventory_policy":"deny","compare_at_price":"9999.00","fulfillment_service":"manual","inventory_management":"shopify","option1":"pile","option2":"various","option3":null,"created_at":"2017-08-01T09:44:11-04:00","updated_at":"2017-08-01T09:44:11-04:00","taxable":true,"barcode":"44569874345654","image_id":null,"inventory_quantity":1,"weight":0.5,"weight_unit":"oz","old_inventory_quantity":1,"requires_shipping":true},{"id":48653472213,"product_id":11400089237,"title":"pile \/ green","price":"1000.00","sku":"12345678974346547000","position":2,"grams":14,"inventory_policy":"deny","compare_at_price":"9999.00","fulfillment_service":"manual","inventory_management":"shopify","option1":"pile","option2":"green","option3":null,"created_at":"2017-08-01T09:44:11-04:00","updated_at":"2017-08-01T09:44:11-04:00","taxable":true,"barcode":"44569874345654","image_id":null,"inventory_quantity":1,"weight":0.5,"weight_unit":"oz","old_inventory_quantity":1,"requires_shipping":true},{"id":48653472277,"product_id":11400089237,"title":"pile \/ blue","price":"1000.00","sku":"12345678974346547000","position":3,"grams":14,"inventory_policy":"deny","compare_at_price":"9999.00","fulfillment_service":"manual","inventory_management":"shopify","option1":"pile","option2":"blue","option3":null,"created_at":"2017-08-01T09:44:11-04:00","updated_at":"2017-08-01T09:44:11-04:00","taxable":true,"barcode":"44569874345654","image_id":null,"inventory_quantity":1,"weight":0.5,"weight_unit":"oz","old_inventory_quantity":1,"requires_shipping":true},{"id":48653472341,"product_id":11400089237,"title":"pile \/ purple","price":"1000.00","sku":"12345678974346547000","position":4,"grams":14,"inventory_policy":"deny","compare_at_price":"9999.00","fulfillment_service":"manual","inventory_management":"shopify","option1":"pile","option2":"purple","option3":null,"created_at":"2017-08-01T09:44:11-04:00","updated_at":"2017-08-01T09:44:11-04:00","taxable":true,"barcode":"44569874345654","image_id":null,"inventory_quantity":1,"weight":0.5,"weight_unit":"oz","old_inventory_quantity":1,"requires_shipping":true},{"id":48653472405,"product_id":11400089237,"title":"pile \/ red","price":"1000.00","sku":"12345678974346547000","position":5,"grams":14,"inventory_policy":"deny","compare_at_price":"9999.00","fulfillment_service":"manual","inventory_management":"shopify","option1":"pile","option2":"red","option3":null,"created_at":"2017-08-01T09:44:11-04:00","updated_at":"2017-08-01T09:44:11-04:00","taxable":true,"barcode":"44569874345654","image_id":null,"inventory_quantity":1,"weight":0.5,"weight_unit":"oz","old_inventory_quantity":1,"requires_shipping":true},{"id":48653472469,"product_id":11400089237,"title":"hill \/ various","price":"1000.00","sku":"12345678974346547000","position":6,"grams":14,"inventory_policy":"deny","compare_at_price":"9999.00","fulfillment_service":"manual","inventory_management":"shopify","option1":"hill","option2":"various","option3":null,"created_at":"2017-08-01T09:44:11-04:00","updated_at":"2017-08-01T09:44:11-04:00","taxable":true,"barcode":"44569874345654","image_id":null,"inventory_quantity":1,"weight":0.5,"weight_unit":"oz","old_inventory_quantity":1,"requires_shipping":true},{"id":48653472533,"product_id":11400089237,"title":"hill \/ green","price":"1000.00","sku":"12345678974346547000","position":7,"grams":14,"inventory_policy":"deny","compare_at_price":"9999.00","fulfillment_service":"manual","inventory_management":"shopify","option1":"hill","option2":"green","option3":null,"created_at":"2017-08-01T09:44:11-04:00","updated_at":"2017-08-01T09:44:11-04:00","taxable":true,"barcode":"44569874345654","image_id":null,"inventory_quantity":1,"weight":0.5,"weight_unit":"oz","old_inventory_quantity":1,"requires_shipping":true},{"id":48653472597,"product_id":11400089237,"title":"hill \/ blue","price":"1000.00","sku":"12345678974346547000","position":8,"grams":14,"inventory_policy":"deny","compare_at_price":"9999.00","fulfillment_service":"manual","inventory_management":"shopify","option1":"hill","option2":"blue","option3":null,"created_at":"2017-08-01T09:44:11-04:00","updated_at":"2017-08-01T09:44:11-04:00","taxable":true,"barcode":"44569874345654","image_id":null,"inventory_quantity":1,"weight":0.5,"weight_unit":"oz","old_inventory_quantity":1,"requires_shipping":true},{"id":48653472661,"product_id":11400089237,"title":"hill \/ purple","price":"1000.00","sku":"12345678974346547000","position":9,"grams":14,"inventory_policy":"deny","compare_at_price":"9999.00","fulfillment_service":"manual","inventory_management":"shopify","option1":"hill","option2":"purple","option3":null,"created_at":"2017-08-01T09:44:11-04:00","updated_at":"2017-08-01T09:44:11-04:00","taxable":true,"barcode":"44569874345654","image_id":null,"inventory_quantity":1,"weight":0.5,"weight_unit":"oz","old_inventory_quantity":1,"requires_shipping":true},{"id":48653472725,"product_id":11400089237,"title":"hill \/ red","price":"1000.00","sku":"12345678974346547000","position":10,"grams":14,"inventory_policy":"deny","compare_at_price":"9999.00","fulfillment_service":"manual","inventory_management":"shopify","option1":"hill","option2":"red","option3":null,"created_at":"2017-08-01T09:44:11-04:00","updated_at":"2017-08-01T09:44:11-04:00","taxable":true,"barcode":"44569874345654","image_id":null,"inventory_quantity":1,"weight":0.5,"weight_unit":"oz","old_inventory_quantity":1,"requires_shipping":true},{"id":48653472789,"product_id":11400089237,"title":"mountain \/ various","price":"1000.00","sku":"12345678974346547000","position":11,"grams":14,"inventory_policy":"deny","compare_at_price":"9999.00","fulfillment_service":"manual","inventory_management":"shopify","option1":"mountain","option2":"various","option3":null,"created_at":"2017-08-01T09:44:11-04:00","updated_at":"2017-08-01T09:44:11-04:00","taxable":true,"barcode":"44569874345654","image_id":null,"inventory_quantity":1,"weight":0.5,"weight_unit":"oz","old_inventory_quantity":1,"requires_shipping":true},{"id":48653472853,"product_id":11400089237,"title":"mountain \/ green","price":"1000.00","sku":"12345678974346547000","position":12,"grams":14,"inventory_policy":"deny","compare_at_price":"9999.00","fulfillment_service":"manual","inventory_management":"shopify","option1":"mountain","option2":"green","option3":null,"created_at":"2017-08-01T09:44:12-04:00","updated_at":"2017-08-01T09:44:12-04:00","taxable":true,"barcode":"44569874345654","image_id":null,"inventory_quantity":1,"weight":0.5,"weight_unit":"oz","old_inventory_quantity":1,"requires_shipping":true},{"id":48653472917,"product_id":11400089237,"title":"mountain \/ blue","price":"1000.00","sku":"12345678974346547000","position":13,"grams":14,"inventory_policy":"deny","compare_at_price":"9999.00","fulfillment_service":"manual","inventory_management":"shopify","option1":"mountain","option2":"blue","option3":null,"created_at":"2017-08-01T09:44:12-04:00","updated_at":"2017-08-01T09:44:12-04:00","taxable":true,"barcode":"44569874345654","image_id":null,"inventory_quantity":1,"weight":0.5,"weight_unit":"oz","old_inventory_quantity":1,"requires_shipping":true},{"id":48653472981,"product_id":11400089237,"title":"mountain \/ purple","price":"1000.00","sku":"12345678974346547000","position":14,"grams":14,"inventory_policy":"deny","compare_at_price":"9999.00","fulfillment_service":"manual","inventory_management":"shopify","option1":"mountain","option2":"purple","option3":null,"created_at":"2017-08-01T09:44:12-04:00","updated_at":"2017-08-01T09:44:12-04:00","taxable":true,"barcode":"44569874345654","image_id":null,"inventory_quantity":1,"weight":0.5,"weight_unit":"oz","old_inventory_quantity":1,"requires_shipping":true},{"id":48653473045,"product_id":11400089237,"title":"mountain \/ red","price":"1000.00","sku":"12345678974346547000","position":15,"grams":14,"inventory_policy":"deny","compare_at_price":"9999.00","fulfillment_service":"manual","inventory_management":"shopify","option1":"mountain","option2":"red","option3":null,"created_at":"2017-08-01T09:44:12-04:00","updated_at":"2017-08-01T09:44:12-04:00","taxable":true,"barcode":"44569874345654","image_id":null,"inventory_quantity":1,"weight":0.5,"weight_unit":"oz","old_inventory_quantity":1,"requires_shipping":true}],"options":[{"id":13812358421,"product_id":11400089237,"name":"Size","position":1,"values":["pile","hill","mountain"]},{"id":13812358485,"product_id":11400089237,"name":"Color","position":2,"values":["various","green","blue","purple","red"]}],"images":[{"id":28069860373,"product_id":11400089237,"position":1,"created_at":"2017-08-01T09:44:54-04:00","updated_at":"2017-08-01T09:44:54-04:00","width":4096,"height":2304,"src":"https:\/\/cdn.shopify.com\/s\/files\/1\/2130\/9415\/products\/20161010_173112.jpg?v=1501595094","variant_ids":[]}],"image":{"id":28069860373,"product_id":11400089237,"position":1,"created_at":"2017-08-01T09:44:54-04:00","updated_at":"2017-08-01T09:44:54-04:00","width":4096,"height":2304,"src":"https:\/\/cdn.shopify.com\/s\/files\/1\/2130\/9415\/products\/20161010_173112.jpg?v=1501595094","variant_ids":[]}},{"id":11319164053,"title":"Test Product","body_html":"This is a test product only for testing API.","vendor":"test_titan","product_type":"","created_at":"2017-07-24T16:05:00-04:00","handle":"test-product","updated_at":"2017-07-25T14:20:01-04:00","published_at":"2017-07-24T16:04:07-04:00","template_suffix":null,"published_scope":"global","tags":"","variants":[{"id":48023373269,"product_id":11319164053,"title":"Default Title","price":"10000.00","sku":"11226644","position":1,"grams":0,"inventory_policy":"deny","compare_at_price":"50.00","fulfillment_service":"manual","inventory_management":"shopify","option1":"Default Title","option2":null,"option3":null,"created_at":"2017-07-24T16:05:00-04:00","updated_at":"2017-07-24T16:05:00-04:00","taxable":true,"barcode":"123456789","image_id":null,"inventory_quantity":20,"weight":0.0,"weight_unit":"lb","old_inventory_quantity":20,"requires_shipping":true}],"options":[{"id":13711000789,"product_id":11319164053,"name":"Title","position":1,"values":["Default Title"]}],"images":[],"image":null}]}
Here is the JSON output that does NOT work:
{"product":{"id":11400089237,"title":"Pile of Rocks","body_html":"Wicked cool pile of rocks from Hawaii!","vendor":"test_titan","product_type":"","created_at":"2017-08-01T09:44:11-04:00","handle":"pile-of-rocks","updated_at":"2017-08-01T09:44:54-04:00","published_at":"2017-08-01T09:40:55-04:00","template_suffix":null,"published_scope":"global","tags":"","variants":[{"id":48653472149,"product_id":11400089237,"title":"pile \/ various","price":"1000.00","sku":"12345678974346547000","position":1,"grams":14,"inventory_policy":"deny","compare_at_price":"9999.00","fulfillment_service":"manual","inventory_management":"shopify","option1":"pile","option2":"various","option3":null,"created_at":"2017-08-01T09:44:11-04:00","updated_at":"2017-08-01T09:44:11-04:00","taxable":true,"barcode":"44569874345654","image_id":null,"inventory_quantity":1,"weight":0.5,"weight_unit":"oz","old_inventory_quantity":1,"requires_shipping":true},{"id":48653472213,"product_id":11400089237,"title":"pile \/ green","price":"1000.00","sku":"12345678974346547000","position":2,"grams":14,"inventory_policy":"deny","compare_at_price":"9999.00","fulfillment_service":"manual","inventory_management":"shopify","option1":"pile","option2":"green","option3":null,"created_at":"2017-08-01T09:44:11-04:00","updated_at":"2017-08-01T09:44:11-04:00","taxable":true,"barcode":"44569874345654","image_id":null,"inventory_quantity":1,"weight":0.5,"weight_unit":"oz","old_inventory_quantity":1,"requires_shipping":true},{"id":48653472277,"product_id":11400089237,"title":"pile \/ blue","price":"1000.00","sku":"12345678974346547000","position":3,"grams":14,"inventory_policy":"deny","compare_at_price":"9999.00","fulfillment_service":"manual","inventory_management":"shopify","option1":"pile","option2":"blue","option3":null,"created_at":"2017-08-01T09:44:11-04:00","updated_at":"2017-08-01T09:44:11-04:00","taxable":true,"barcode":"44569874345654","image_id":null,"inventory_quantity":1,"weight":0.5,"weight_unit":"oz","old_inventory_quantity":1,"requires_shipping":true},{"id":48653472341,"product_id":11400089237,"title":"pile \/ purple","price":"1000.00","sku":"12345678974346547000","position":4,"grams":14,"inventory_policy":"deny","compare_at_price":"9999.00","fulfillment_service":"manual","inventory_management":"shopify","option1":"pile","option2":"purple","option3":null,"created_at":"2017-08-01T09:44:11-04:00","updated_at":"2017-08-01T09:44:11-04:00","taxable":true,"barcode":"44569874345654","image_id":null,"inventory_quantity":1,"weight":0.5,"weight_unit":"oz","old_inventory_quantity":1,"requires_shipping":true},{"id":48653472405,"product_id":11400089237,"title":"pile \/ red","price":"1000.00","sku":"12345678974346547000","position":5,"grams":14,"inventory_policy":"deny","compare_at_price":"9999.00","fulfillment_service":"manual","inventory_management":"shopify","option1":"pile","option2":"red","option3":null,"created_at":"2017-08-01T09:44:11-04:00","updated_at":"2017-08-01T09:44:11-04:00","taxable":true,"barcode":"44569874345654","image_id":null,"inventory_quantity":1,"weight":0.5,"weight_unit":"oz","old_inventory_quantity":1,"requires_shipping":true},{"id":48653472469,"product_id":11400089237,"title":"hill \/ various","price":"1000.00","sku":"12345678974346547000","position":6,"grams":14,"inventory_policy":"deny","compare_at_price":"9999.00","fulfillment_service":"manual","inventory_management":"shopify","option1":"hill","option2":"various","option3":null,"created_at":"2017-08-01T09:44:11-04:00","updated_at":"2017-08-01T09:44:11-04:00","taxable":true,"barcode":"44569874345654","image_id":null,"inventory_quantity":1,"weight":0.5,"weight_unit":"oz","old_inventory_quantity":1,"requires_shipping":true},{"id":48653472533,"product_id":11400089237,"title":"hill \/ green","price":"1000.00","sku":"12345678974346547000","position":7,"grams":14,"inventory_policy":"deny","compare_at_price":"9999.00","fulfillment_service":"manual","inventory_management":"shopify","option1":"hill","option2":"green","option3":null,"created_at":"2017-08-01T09:44:11-04:00","updated_at":"2017-08-01T09:44:11-04:00","taxable":true,"barcode":"44569874345654","image_id":null,"inventory_quantity":1,"weight":0.5,"weight_unit":"oz","old_inventory_quantity":1,"requires_shipping":true},{"id":48653472597,"product_id":11400089237,"title":"hill \/ blue","price":"1000.00","sku":"12345678974346547000","position":8,"grams":14,"inventory_policy":"deny","compare_at_price":"9999.00","fulfillment_service":"manual","inventory_management":"shopify","option1":"hill","option2":"blue","option3":null,"created_at":"2017-08-01T09:44:11-04:00","updated_at":"2017-08-01T09:44:11-04:00","taxable":true,"barcode":"44569874345654","image_id":null,"inventory_quantity":1,"weight":0.5,"weight_unit":"oz","old_inventory_quantity":1,"requires_shipping":true},{"id":48653472661,"product_id":11400089237,"title":"hill \/ purple","price":"1000.00","sku":"12345678974346547000","position":9,"grams":14,"inventory_policy":"deny","compare_at_price":"9999.00","fulfillment_service":"manual","inventory_management":"shopify","option1":"hill","option2":"purple","option3":null,"created_at":"2017-08-01T09:44:11-04:00","updated_at":"2017-08-01T09:44:11-04:00","taxable":true,"barcode":"44569874345654","image_id":null,"inventory_quantity":1,"weight":0.5,"weight_unit":"oz","old_inventory_quantity":1,"requires_shipping":true},{"id":48653472725,"product_id":11400089237,"title":"hill \/ red","price":"1000.00","sku":"12345678974346547000","position":10,"grams":14,"inventory_policy":"deny","compare_at_price":"9999.00","fulfillment_service":"manual","inventory_management":"shopify","option1":"hill","option2":"red","option3":null,"created_at":"2017-08-01T09:44:11-04:00","updated_at":"2017-08-01T09:44:11-04:00","taxable":true,"barcode":"44569874345654","image_id":null,"inventory_quantity":1,"weight":0.5,"weight_unit":"oz","old_inventory_quantity":1,"requires_shipping":true},{"id":48653472789,"product_id":11400089237,"title":"mountain \/ various","price":"1000.00","sku":"12345678974346547000","position":11,"grams":14,"inventory_policy":"deny","compare_at_price":"9999.00","fulfillment_service":"manual","inventory_management":"shopify","option1":"mountain","option2":"various","option3":null,"created_at":"2017-08-01T09:44:11-04:00","updated_at":"2017-08-01T09:44:11-04:00","taxable":true,"barcode":"44569874345654","image_id":null,"inventory_quantity":1,"weight":0.5,"weight_unit":"oz","old_inventory_quantity":1,"requires_shipping":true},{"id":48653472853,"product_id":11400089237,"title":"mountain \/ green","price":"1000.00","sku":"12345678974346547000","position":12,"grams":14,"inventory_policy":"deny","compare_at_price":"9999.00","fulfillment_service":"manual","inventory_management":"shopify","option1":"mountain","option2":"green","option3":null,"created_at":"2017-08-01T09:44:12-04:00","updated_at":"2017-08-01T09:44:12-04:00","taxable":true,"barcode":"44569874345654","image_id":null,"inventory_quantity":1,"weight":0.5,"weight_unit":"oz","old_inventory_quantity":1,"requires_shipping":true},{"id":48653472917,"product_id":11400089237,"title":"mountain \/ blue","price":"1000.00","sku":"12345678974346547000","position":13,"grams":14,"inventory_policy":"deny","compare_at_price":"9999.00","fulfillment_service":"manual","inventory_management":"shopify","option1":"mountain","option2":"blue","option3":null,"created_at":"2017-08-01T09:44:12-04:00","updated_at":"2017-08-01T09:44:12-04:00","taxable":true,"barcode":"44569874345654","image_id":null,"inventory_quantity":1,"weight":0.5,"weight_unit":"oz","old_inventory_quantity":1,"requires_shipping":true},{"id":48653472981,"product_id":11400089237,"title":"mountain \/ purple","price":"1000.00","sku":"12345678974346547000","position":14,"grams":14,"inventory_policy":"deny","compare_at_price":"9999.00","fulfillment_service":"manual","inventory_management":"shopify","option1":"mountain","option2":"purple","option3":null,"created_at":"2017-08-01T09:44:12-04:00","updated_at":"2017-08-01T09:44:12-04:00","taxable":true,"barcode":"44569874345654","image_id":null,"inventory_quantity":1,"weight":0.5,"weight_unit":"oz","old_inventory_quantity":1,"requires_shipping":true},{"id":48653473045,"product_id":11400089237,"title":"mountain \/ red","price":"1000.00","sku":"12345678974346547000","position":15,"grams":14,"inventory_policy":"deny","compare_at_price":"9999.00","fulfillment_service":"manual","inventory_management":"shopify","option1":"mountain","option2":"red","option3":null,"created_at":"2017-08-01T09:44:12-04:00","updated_at":"2017-08-01T09:44:12-04:00","taxable":true,"barcode":"44569874345654","image_id":null,"inventory_quantity":1,"weight":0.5,"weight_unit":"oz","old_inventory_quantity":1,"requires_shipping":true}],"options":[{"id":13812358421,"product_id":11400089237,"name":"Size","position":1,"values":["pile","hill","mountain"]},{"id":13812358485,"product_id":11400089237,"name":"Color","position":2,"values":["various","green","blue","purple","red"]}],"images":[{"id":28069860373,"product_id":11400089237,"position":1,"created_at":"2017-08-01T09:44:54-04:00","updated_at":"2017-08-01T09:44:54-04:00","width":4096,"height":2304,"src":"https:\/\/cdn.shopify.com\/s\/files\/1\/2130\/9415\/products\/20161010_173112.jpg?v=1501595094","variant_ids":[]}],"image":{"id":28069860373,"product_id":11400089237,"position":1,"created_at":"2017-08-01T09:44:54-04:00","updated_at":"2017-08-01T09:44:54-04:00","width":4096,"height":2304,"src":"https:\/\/cdn.shopify.com\/s\/files\/1\/2130\/9415\/products\/20161010_173112.jpg?v=1501595094","variant_ids":[]}}}
I can see the main difference between the two is the square brackets [], but I still can't figure out why it's not parsing the second one. Any help is greatly appreciated.
procedure TForm1.parsejson2(s, sKey: string);
var
jsv : TJsonValue;
originalObject : TJsonObject;
jsPair : TJsonPair;
jsArr : TJsonArray;
jso : TJsonObject;
i,j : integer;
aos : Tarray<string>;
sValue : String;
begin
try
//parse json string
jsv := TJSONObject.ParseJSONValue(s);
try
//value as object
originalObject := jsv as TJsonObject;
//get pair, which contains Array of objects
jspair := originalObject.Get(sKey);
//pair value as array
jsArr := jsPair.jsonValue as TJsonArray;
//enumerate objects in array
for i := 0 to jsArr.Size - 1 do
begin // i-th object
try
jso := jsArr.Get(i) as TJsonObject;
except
end;
//enumerate object fields
for jsPair in jso do
begin
Memo2.Lines.Add(spaces + ' '+ jsPair.JsonString.Value+ ': '+ jsPair.JsonValue.Value);
if (jsPair.JsonValue is TJsonArray) then
begin
spaces := spaces + ' ';
if (jsPair.JsonValue.ToString).Contains(':') then
begin
ParseJson2('{"'+jsPair.JsonString.Value+'":'+jsPair.JsonValue.ToString+'}', jsPair.JsonString.Value);
end else begin
aos := (jsPair.JsonValue.ToString).Split([','], 99);
for j := low(aos) to High(aos) do
begin
sValue := StringReplace(aos[j], '[', '', [rfReplaceAll]);
sValue := StringReplace(sValue, ']', '', [rfReplaceAll]);
sValue := StringReplace(sValue, '"', '', [rfReplaceAll]);
if trim(sValue) <> '' then Memo2.Lines.Add(spaces+spaces+sValue);
end;
end;
spaces := copy(spaces,1,length(spaces)-3);
end;
end;
end;
finally
jsv.Free();
end;
except
on E: Exception do
Memo2.Lines.Add(E.ClassName+ ': '+ E.Message);
end;
end;
It looks like the Delphi code is parsing everything just fine. The problem comes from your code. The brackets in the JSON aren't just for show. The first result you get, which works, has a products
value that is an array of objects. Thus, this line works:
jsArr := jsPair.jsonValue as TJsonArray;
The second result you get, which doesn't work, has a product
value (not products
) that is not an array, it is a single object, so when you run that line of code, you have indeed executed an invalid class typecast. You would need to use jsPair.jsonValue as TJsonObject
instead.
If I were you, I wouldn't try to handle both kinds of results with the same code. I'd factor out the code that deals with a single product and put that code in its own function. Then I'd write two JSON-processing functions:
One would read the first kind of JSON result you showed, and call the product-handling function in a loop for each object in the products
array.
The other would read the second kind of JSON result you showed, and call the product-handling function for the single product
object.