[Plain]View PlainCopyprint?
- <span style= "FONT-SIZE:14PX;" > has been looking for how to parse the JSON data, today finally found someone posted. It was previously said to use Superobject,tlkjson,delphi JSON Library,delphi Web unit and so on. In fact, I'm looking for a simpler way to parse. Parse the simple JSON. The following is a reprint of the calm source.
- </span>
[Delphi]View PlainCopyprint?
- Unit Unit1;
- Interface
- Uses
- Winapi. Windows, Winapi. Messages, System. Sysutils, System. Variants, System. Classes, Vcl. Graphics,
- VCL. Controls, Vcl. Forms, Vcl. Dialogs, Vcl. Stdctrls,dbxjson;
- Type
- TForm1 = Class (Tform)
- Button1:tbutton;
- procedure Button1Click (sender:tobject);
- Private
- {Private declarations}
- Public
- {public declarations}
- end;
- Var
- Form1:tform1;
- Implementation
- {$R *.DFM}
- Const
- Gjsonstring =
- ' {' +
- ' ' name ': {' +
- ' A JSON Object ': {' +
- ' id ': ' 1 ' +
- '}, ' +
- ' Another JSON Object ': {' +
- ' id ': ' 2 ' +
- '} ' +
- '}, ' +
- ' "Totalobjects": "2" ' +
- '} ';
- Procedure TForm1. Button1Click (Sender:tobject);
- Var
- Ljsonobject:tjsonobject;
- Value:tjsonvalue;
- Begin
- Ljsonobject: = nil;
- Try
- Ljsonobject: = Tjsonobject. Create;
- Value: = Tjsonvalue. Create;
- {convert String to JSON}
- Ljsonobject. Parse (Bytesof (gjsonstring), 0);
- Value: =ljsonobject. GetValue (' name ');
- ShowMessage (Value. ToString);
- finally
- Ljsonobject. Free;
- end;
- End
- End.
Gray often good, here thank Bo Master.
However, there will be garbled characters when dealing with Chinese in this way. I modified the code a little bit:
[Delphi]View PlainCopy print?
- Var
- Jo:tjsonobject;
- Jv:tjsonvalue;
- JSONSTR:string; The JSON string to convert
- Begin
- jo:=Nil;
- Jsonstr:= '{"name": "Chuan Feng", "interest": "Quarrel with Cherry Wood"};
- Try
- Jo:=tjsonobject. Create;
- Jo:=tjsonobjectparsejsonvalue (tencoding. UTF8GetBytes (jsonstr),0) as tjsonobject;
- Jv:=jo. Get (' interest '). Jsonvalue;
- ShowMessage (JV. Value);
- finally
- Jo. Free;
- end;
- End
can finally be converted into Chinese.
The following is an analysis of Jsonobject (extrapolate):
[Delphi]View PlainCopyprint?
- Procedure TForm1. Button1Click (Sender:tobject);
- Var
- JSONSTR: string;
- Jvalue:tjsonvalue;
- Jobj:tjsonobject;
- Jpair:tjsonpair;
- Jarray:tjsonarray;
- Begin
- jsonstr:=' {' name': ' Tom', ' Password': ' Tomcat', ' Interests': [' Mouse', ' Meat']} ';
- Jvalue: = Tjsonobject. Parsejsonvalue
- (Tencoding. UTF8. GetBytes (jsonstr), 0);
- Try
- Jobj: = Jvalue as tjsonobject;
- Jpair: = Jobj. Get (2); //Get the third JSON pair
- Jarray: = Jpair. Jsonvalue as Tjsonarray; //Pair value is an array [' mouse ', ' meat ']
- Strresult: = Jarray. Get (0). value; //First element of array[' mouse ', ' meat ']
- ShowMessage (strresult); //it is mouse
- finally
- Jvalue. Free;
- end;
- end;
http://blog.csdn.net/syndicater/article/details/17371111
How to parse the JSON data returned by Delphi XE5 Server (translation) and Chinese garbled characters