I am studying the VC API and writing software to work with him. All parsing json-on with VK API I put on SuperObject. And he did an excellent job with his task, but let him down on a stress test. I fed him this json:

{"response": {"count": 204, "items": [{"id": 154770, "body": "", "user_id": 1, "from_id": 1, "date": 1492002564, " read_state ": 1," out ": 0," random_id ": 965793707," fwd_messages ": [{" user_id ": 1," date ": 1492002563," body ":" "," fwd_messages ": [{" user_id ": 1," date ": 1492002561," body ":" cake "," fwd_messages ": [{" user_id ": 1," date ": 1492002534," body ":" lol "," fwd_messages ": [{ "user_id": 1, "date": 1492002530, "body": "", "fwd_messages": [{"user_id": 1, "date": 1492002528, "body": "", "fwd_messages": [{ "user_id": 1, "date": 1492002512, "body": "", "fwd_messages": [{"user_id": 1, "date": 1492002510, "body": "", "fwd_messages": [{ "user_id": 1, "date": 1492002508, "body": "", "fwd_messages": [{"user_id": 1, "date": 1492002506, "body": "", "fwd_messages": [{ "user_id": 1, "date": 1492002504, "body": "", "fwd_messages": [{"user_id": 1, "date": 1492002499, "body": "", "fwd_messages": [{ "user_id": 1, "date": 1492002497, "body": "", "fwd_messages": [{"user_id": 1, "date": 1492002494, "body": "", "fwd_messages": [{ "user_id": 1, "date": 1492002492, "body": "", "fwd_messages": [{"user_id": 1, "date": 1492002490 , "body": "", "fwd_messages": [{"user_id": 1, "date": 1492002488, "body": "", "fwd_messages": [{"user_id": 1, "date": 1492002486 , "body": "", "fwd_messages": [{"user_id": 1, "date": 1491925392, "body": "", "fwd_messages": [{"user_id": 1, "date": 1490901947 , "body": "test message"}]]]}]}]}]}]}]}]}]}]}]}]}]}]}]}]]]}]}]}], "in_read": 155942, "out_read": 155942}}

As a result, when you try to parse it, it just falls with an error. Of course, I could catch errors and just go ahead in case of an error, but unfortunately, this does not suit me. By this, I first began to look towards finding new libraries. And I stumbled upon the uLkJSON library. And everything is fine, it fully complies with the requirements, but here is bad luck .. I need to return not just some parameters from jsona, but to transfer a whole json object from the array of the form: [{...},{...},{...}] . It is with curly brackets and everything as needed. and all this is converted into a string, so that its function is further parsed. But as I did not fight, I just can not do it. I tried to pull out like this:

 TlkJSON.ParseText(response_loadmessage).Field['response'].field['items'].Child[0].Value; 

But in the end, I fall with an error. I can not understand what the problem is and nowhere can I find a solution. I really hope for your help, and in case of anything, offers me other libraries to work with json.

  • one
    You have items - an array of one object, and not an array of objects. Do you need to get it? With the standard parser that is built into Delphi, this object gets pretty easy: pastebin.com/ENmWSuGL - zed
  • It is in this example, such an array, usually inside it 200 objects and each must be pulled out. (I'll be back home in a couple of hours and will deal with your examples) - Voen Upyachki
  • Always specify the text of the error and the code that reproduces it - Kromster

1 answer 1

Here is an example implementation using a recursive call to the parsing procedure. Threw a quick ...

 unit Unit1; interface uses System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, System.JSON, FMX.Controls.Presentation, FMX.ScrollBox, FMX.Memo; const cJsonSource : string = '{"response":{"count":204,"items":[{"id":154770,"body":"","user_id":1,'+ '"from_id":1,"date":1492002564,"read_state":1,"out":0,"random_id":965793707,'+ '"fwd_messages":[{"user_id":1,"date":1492002563,"body":"","fwd_messages":'+ '[{"user_id":1,"date":1492002561,"body":"кек","fwd_messages":[{"user_id":1,'+ '"date":1492002534,"body":"лол","fwd_messages":[{"user_id":1,"date":'+ '1492002530,"body":"","fwd_messages":[{"user_id":1,"date":1492002528,'+ '"body":"","fwd_messages":[{"user_id":1,"date":1492002512,"body":"",'+ '"fwd_messages":[{"user_id":1,"date":1492002510,"body":"","fwd_messages":'+ '[{"user_id":1,"date":1492002508,"body":"","fwd_messages":[{"user_id":1,'+ '"date":1492002506,"body":"","fwd_messages":[{"user_id":1,"date":1492002504,'+ '"body":"","fwd_messages":[{"user_id":1,"date":1492002499,"body":"",'+ '"fwd_messages":[{"user_id":1,"date":1492002497,"body":"","fwd_messages"'+ ':[{"user_id":1,"date":1492002494,"body":"","fwd_messages":[{"user_id":1,'+ '"date":1492002492,"body":"","fwd_messages":[{"user_id":1,"date":1492002490,'+ '"body":"","fwd_messages":[{"user_id":1,"date":1492002488,"body":"",'+ '"fwd_messages":[{"user_id":1,"date":1492002486,"body":"","fwd_messages":'+ '[{"user_id":1,"date":1491925392,"body":"","fwd_messages":[{"user_id":1,'+ '"date":1490901947,"body":"test message"}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}],'+ '"in_read":155942,"out_read":155942}}'; type TForm1 = class(TForm) Memo1: TMemo; procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } procedure ParseArrayMessage(AJSONArrayMessage : TJSONArray; Var ARecursiveDeep : Integer); procedure ParseMessage(AJSONMessage : TJSONObject; Var ARecursiveDeep : Integer); end; var Form1: TForm1; implementation {$R *.fmx} procedure TForm1.FormCreate(Sender: TObject); Var LJSON : TJSONObject; LJSONArray : TJSONArray; LMessageCount : Integer; LRecursiveDeep : Integer; begin LJSON:=TJSONObject(TJSONObject.ParseJSONValue(cJsonSource)); if Not Assigned(LJSON) then Exit; if Not LJSON.TryGetValue('response', LJSON) then Exit; if LJSON.TryGetValue('count', LMessageCount) then Memo1.Lines.Add('Количество сообщений: '+LMessageCount.ToString); LRecursiveDeep:=0; if LJSON.TryGetValue('items', LJSONArray) then if Assigned(LJSONArray) then ParseArrayMessage(LJSONArray, LRecursiveDeep); end; procedure TForm1.ParseArrayMessage(AJSONArrayMessage : TJSONArray; Var ARecursiveDeep : Integer); Var I : Integer; LJSON : TJSONObject; begin for I := 0 to AJSONArrayMessage.Count-1 do begin LJSON:=TJSONObject(AJSONArrayMessage.Items[I]); if Assigned(LJSON) then ParseMessage(LJSON, ARecursiveDeep); end; end; procedure TForm1.ParseMessage(AJSONMessage : TJSONObject; Var ARecursiveDeep : Integer); Var I : Integer; LName, LValue : String; LJSONArray : TJSONArray; LIndent : String; begin Inc(ARecursiveDeep); // Увеличиваем счетчик рекурсивного погружения (для красивого отступа) for I := 0 to AJSONMessage.Count-1 do begin LName:=AJSONMessage.Pairs[I].JsonString.Value; if Not (AJSONMessage.Pairs[I].JsonValue is TJSONArray) then //Если элемент не JSON массив begin LValue:=AJSONMessage.Pairs[I].JsonValue.Value; LIndent:=LIndent.PadLeft(ARecursiveDeep * 4); Memo1.Lines.Add(LIndent + LName + '=' + LValue); end Else begin LJSONArray:=TJSONArray(AJSONMessage.Pairs[I].JsonValue); if Assigned(LJSONArray) then ParseArrayMessage(LJSONArray, ARecursiveDeep); // Рекурсивный вызов end; end; end; end. 

  • one
    And working? Add a few words as it will help the author of the question. - 0xdb