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?

Comments

  1. I borrowed and adapted the code from http://stackoverflow.com/questions/19415616/how-to-parse-specified-value-from-json-object-in-delphi-xe3

    But at
    LJsonObj    := TJSONObject.ParseJSONValue(TEncoding.ASCII.GetBytes(StrJson),0) as TJSONObject;

    I get an Invalid Typecast error.

    ReplyDelete
  2. it's an array?
    it's an object?
    try removing all the [ ] and pasting it again

    ReplyDelete
  3. That's confusing me. I don't know the JSON I get is enclosed within []

    ReplyDelete
  4. Put 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.

    ReplyDelete
  5. Matteo Luigi Riso Which code? That SO question has two answers, one using SuperObject, the other using DBXJSON.

    ReplyDelete
  6. I'm sorry (I'm not confident with JSON): the DBXJSON version

    ReplyDelete
  7. Matteo Luigi Riso  Which XE version you are using?  I may help you to for demo codes if it is XE 6.

    ReplyDelete
  8. Matteo Luigi Riso this code works fine for me

    program 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.

    ReplyDelete
  9. I'm on XE2. It seems I have two JSON nested arrays...

    ReplyDelete
  10. Matteo Luigi Riso
    yes you do have 2 arrays here, my code above was written in xe2.

    ReplyDelete
  11. Thank you very much Andrew Terekhov . That works fine!

    ReplyDelete
  12. Matteo Luigi Riso   you're welcome! glad to help!

    ReplyDelete
  13. 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

    ReplyDelete
  14. A. Bouchez are there any speed comparison tests results you can point us to ?

    ReplyDelete

Post a Comment