Need some help with parsing JSON.
Need some help with parsing JSON.
The server returns this JSON string.
{"version":3,"status":"ok","response":{"data":[{"avg_price":32.16,"brand":"Krusteaz","product_name":"Muffin Mix Wild Blueberry","size":["17.1 oz"]}],"included_rows":1}}
Using Andreas Hausladen's JsonDataObjects. XE8 trial. Will be used on Android phones.
This code does not complain but neither does it return the values:]
var
avg: string;
Obj: TJsonObject;
AryRoot, Ary, AryData, arysize: TJsonArray;
begin
Obj := TJsonObject.Parse(Resp) as TJsonObject;
try
if obj.Values['status'] = 'ok' then
begin
log(liapp, 'status = ok');
avg_price := obj.Values['avg_price'].IntValue;
brand := obj.values['brand'].Value;
product_name := obj.Values['product_name'].Value;
log(liapp, 'Parse: product_name: ' + product_name + ', size: ' + size);
finally
obj.Free;
end;
This code runs but gives a 'Invalid class typecast'
var
avg: string;
Obj: TJsonObject;
AryRoot, Ary, AryData, arysize: TJsonArray;
begin
Obj := TJsonObject.Parse(Resp) as TJsonObject;
try
if obj.Values['status'] = 'ok' then
begin
try
aryroot := TJsonArray.Parse(obj.ToString) as TJsonArray;
try
arydata := TJsonArray.Parse(aryroot.ToString) as TJsonArray;
avg_price := arydata.Values[0].IntValue;
brand := ary.Values[1].value;
product_name:= ary.Values[2].value;
try
arysize := TJsonArray.Parse(arydata.ToString) as TJsonArray;
size := ary.Values[0].value;
...
Can someone show me how to extract the values?
Thanks...
The server returns this JSON string.
{"version":3,"status":"ok","response":{"data":[{"avg_price":32.16,"brand":"Krusteaz","product_name":"Muffin Mix Wild Blueberry","size":["17.1 oz"]}],"included_rows":1}}
Using Andreas Hausladen's JsonDataObjects. XE8 trial. Will be used on Android phones.
This code does not complain but neither does it return the values:]
var
avg: string;
Obj: TJsonObject;
AryRoot, Ary, AryData, arysize: TJsonArray;
begin
Obj := TJsonObject.Parse(Resp) as TJsonObject;
try
if obj.Values['status'] = 'ok' then
begin
log(liapp, 'status = ok');
avg_price := obj.Values['avg_price'].IntValue;
brand := obj.values['brand'].Value;
product_name := obj.Values['product_name'].Value;
log(liapp, 'Parse: product_name: ' + product_name + ', size: ' + size);
finally
obj.Free;
end;
This code runs but gives a 'Invalid class typecast'
var
avg: string;
Obj: TJsonObject;
AryRoot, Ary, AryData, arysize: TJsonArray;
begin
Obj := TJsonObject.Parse(Resp) as TJsonObject;
try
if obj.Values['status'] = 'ok' then
begin
try
aryroot := TJsonArray.Parse(obj.ToString) as TJsonArray;
try
arydata := TJsonArray.Parse(aryroot.ToString) as TJsonArray;
avg_price := arydata.Values[0].IntValue;
brand := ary.Values[1].value;
product_name:= ary.Values[2].value;
try
arysize := TJsonArray.Parse(arydata.ToString) as TJsonArray;
size := ary.Values[0].value;
...
Can someone show me how to extract the values?
Thanks...
Comments
Post a Comment