thomaserlang / delphi-json

JSON parser for Delphi.
83 stars 32 forks source link

JSON problems #1

Closed santidat closed 10 years ago

santidat commented 10 years ago

I just check your JSON parser with Delphi using the SAMPLE 2 (with the same data ) and its works FINE. [ { "username": "thomas", "name": "Thomas" }, { "username": "kurt", "name": "Kurt" }, { "username": "bent", "name": "Bent" } ]

var users: TJSON; user: TJSON; begin users := TJSON.Parse({JSON_TEXT}); try for user in users do begin writeln(user['username'].AsString); writeln(user['name'].AsString); end; finally users.Free; end; end;

But When I use the JSON parser with FireMonkey and test in an Android deployment -using the same data like before- I have an error "unknow field: username"

I debug both samples and I found this difference :

* debugging lines, where the ERROR raise * case VarType(AData) and VarTypeMask of varString, varUString, varWord, varLongWord: if not FItems.TryGetValue(AData, result) then

    raise EJSONUnknownFieldOrIndex.Create(format('Unknown field: %s', [AData]))

  else
    exit;

I run this code Thomas wrote and I download the new JSON library :

var user: TJSON; s: string; begin with TJSON.Parse({JSON DATA}) do begin try showmessage(inttostr(Items.count)); for s in items.Keys do ShowMessage(s); finally Free; end; end; end;

... and did not worked. But I changed the string taking off the initial and end '[' ']' and now the new code ,the last one, is working fine, but the example raises an internal code ,not JSON class code like this "Access violation at address 5393dbfa , accessing address 0000000C"

Thanks in advance for your time and help.

thomaserlang commented 10 years ago

So in the items.count is 0?

santidat commented 10 years ago

Hello: I submit an issue, but for your question I think I did not answer in a proper English way.Sorry!!! I am experimenting a lot and I can tell you several things.

1.- if the string is built in this way a:='[{"name","adams"}, ..... ]' the code you sent me not work. 2.- If I change the string and I don't use the [] , the code you sent me will print 2 records count and print "username" and "name"

BUt the JSON class writen like your EXAMPLE 2 doesn't work. (taking off the [] and show another error menssage)

On the other hand I observed this:

When I debug the Delphi program ...

case VarType(AData) and VarTypeMask of varString, varUString, varWord, varLongWord: if not FItems.TryGetValue(VartoStr(AData), result) then raise EJSONUnknownFieldOrIndex.Create(format('Unknown field: %s', [AData]))

Adata='e' (I changed letters to test)

But with the mobile Firemonkey (part of RAD Studio XE5 and based in Delphi ) in the same place Adata=0x547e17c8

I read this web page http://docwiki.embarcadero.com/RADStudio/XE6/en/Migrating_Delphi_Code_to_Mobile_from_Desktopand check but I did not find anything. And I think is a format issue.

Thanks again for your help and time!!!

2014-04-22 19:02 GMT+02:00 Thomas Erlang notifications@github.com:

So in the items.count is 0?

— Reply to this email directly or view it on GitHubhttps://github.com/thomaserlang/delphi-json/issues/1#issuecomment-41066034 .

thomaserlang commented 10 years ago

Ah, correct, the code snippet i send you only works for a JSON object, what you tested it against was a list of objects.

If the TJSON object is a list the Items parameter is unassigned.

To check if an JSON object is a list use the IsList parameter.

A list of the items is stored in the ListItems (when the object is a list). If the TJSON object is not a list, ListItems will be unassigned.

The code snippet i send you can be rewritten to write out all the fields in a list:

procedure test();
var
  user, users: TJSON;
  s: string;
begin
  users := TJSON.Parse('[{"username":"user1"},{"username":"user2"}]');
  try
    for user in users do
    begin
      showmessage(format('Item: %d', [users.ListItems.IndexOf(user)]));
      showmessage(inttostr(user.Items.count));
      for s in user.Items.Keys do
        ShowMessage(s);
    end;
  finally
    users.Free;
  end;
end;
thomaserlang commented 10 years ago

I believe that i have fixed your problem with this commit: https://github.com/thomaserlang/delphi-json/commit/7847f1b34e245ee873c86869f1670cffc0383447.

The error was, that i was using the apparently outdated 1-based string version in the unescape function. XE3 and up has added TStringHelper which addes 0-based strings, which is the only form on mobile1.

santidat commented 10 years ago

Thomas:

Like allways. .. THANKS for the answer.

I just read yor message , re-write the JSON class and try it . IT WORKS!!!!!

I use the JSON string with [ and ] , no changes needed .

You are very kind, very fast and very SMART.

With best wishes and regard :

Santiago

2014-04-23 10:05 GMT+02:00 Thomas Erlang notifications@github.com:

Closed #1 https://github.com/thomaserlang/delphi-json/issues/1.

— Reply to this email directly or view it on GitHubhttps://github.com/thomaserlang/delphi-json/issues/1 .