Hello again.
Hello again.
I'm going mad with this JSON result:
[
{
"type": "qrcode",
"symbol": [
{
"seq": 0,
"data": "mydata",
"error": null
}
]
}
]
I can't parse the "data" item to get the "mydata" value.
Do you have any code to solve this?
I'm going mad with this JSON result:
[
{
"type": "qrcode",
"symbol": [
{
"seq": 0,
"data": "mydata",
"error": null
}
]
}
]
I can't parse the "data" item to get the "mydata" value.
Do you have any code to solve this?
What Delphi code do you have so far?
ReplyDeleteI borrowed and adapted the code from http://stackoverflow.com/questions/19415616/how-to-parse-specified-value-from-json-object-in-delphi-xe3
ReplyDeleteBut at
LJsonObj := TJSONObject.ParseJSONValue(TEncoding.ASCII.GetBytes(StrJson),0) as TJSONObject;
I get an Invalid Typecast error.
it's an array?
ReplyDeleteit's an object?
try removing all the [ ] and pasting it again
That's confusing me. I don't know the JSON I get is enclosed within []
ReplyDeletePut the JSON into something like http://jsonutils.com/ or http://json2csharp.com/ to see what the underlying structure should be like, then adapt your Delphi code accordingly.
ReplyDeleteMatteo Luigi Riso Which code? That SO question has two answers, one using SuperObject, the other using DBXJSON.
ReplyDeleteI'm sorry (I'm not confident with JSON): the DBXJSON version
ReplyDeleteMatteo Luigi Riso Which XE version you are using? I may help you to for demo codes if it is XE 6.
ReplyDeleteMatteo Luigi Riso this code works fine for me
ReplyDeleteprogram Project4;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils, dbxjson;
const DATA = '[{"type": "qrcode","symbol": [{"seq": 0,"data": "mydata","error": null}]}]';
var ja,s : TJsonArray;
jv, v : TJsonValue;
jp : TJsonPair;
begin
ja := TJsonObject.ParseJSONValue(DATA) as TJsonArray;
for jv in ja do begin
s := (jv as TJsonObject).Get('symbol').JsonValue as TJsonArray;
for v in s do begin
jp := (v as TJsonObject).Get('data');
writeln('key: ', jp.JsonString.ToString, ' value: ', jp.JsonValue.ToString);
end;
end;
readln;
end.
I'm on XE2. It seems I have two JSON nested arrays...
ReplyDeleteMatteo Luigi Riso
ReplyDeleteyes you do have 2 arrays here, my code above was written in xe2.
Thank you very much Andrew Terekhov . That works fine!
ReplyDeleteMatteo Luigi Riso you're welcome! glad to help!
ReplyDeleteSuperObject is good parser JSON.
ReplyDeletehttps://code.google.com/p/superobject/downloads/list
Or SynCrossPlatformJSON.pas which outperforms them in performance and allow access via variants and late binding. See https://github.com/synopse/mORMot/blob/master/CrossPlatform/SynCrossPlatformJSON.pas
ReplyDeleteA. Bouchez are there any speed comparison tests results you can point us to ?
ReplyDelete