Using JSON in Delphi XE7

Source: Internet
Author: User

Delphi XE7 has a JSON-processing unit that introduces "System.json" in the cells you need to use JSON, and then you can use Delphi's own JSON processing classes. My small example is just parsing the JSON containing strings and arrays, two data types that I think are the most commonly used and useful types in actual use, so I just use these two examples to do the demo!

Demo Code:

{function: Use JSON in DelphiXE7------------------------------------------------------------------------------Description: 1, Use Delphi's own JSON (System.json). 2, this is just a simple example, and will add the demo function later. ------------------------------------------------------------------------------Note: After the 1,json class is created, all elements inside are freed without tubes, The JSON class manages itself, must not the superfluous lily!!!!!! ------------------------------------------------------------------------------Author: Sun Yuliang qq:14667479 Email:[email Protected] Modified: 2014/11/23 00:13------------------------------------------------------------------------------ Development tool: Delphi XE7 test Mobile: Huawei Glory X1}UnitUnit1;Interfaceusessystem.sysutils, System.types, System.uitypes, SYSTEM.CLASSES,SYSTEM.VARIANTS,FMX. Types, FMX. Controls, FMX. Forms, FMX. Graphics, FMX. Dialogs, FMX. STDCTRLS,FMX. Layouts, FMX. Memo;typeTForm1=class(tform) Panel1:tpanel; Memo1:tmemo; Panel2:tpanel; Button1:tbutton; Button2:tbutton; Memo2:tmemo; Button3:tbutton;procedureButton1Click (sender:tobject);procedureButton2click (sender:tobject);procedureformcreate (sender:tobject);procedureButton3click (sender:tobject);procedureformresize (sender:tobject);Private{Private Declarations}//reset ButtonprocedureResetbutton; Public{Public Declarations}End;varForm1:tform1;Const//JSON for PresentationJsonstring ='{"name": "Zhang San", "Other": ["China", "Programmer"]}';Implementation{$R *.FMX}usesSystem.json;//Dephi Self-brought JSON unitprocedureTform1.button1click (sender:tobject);varJsonobject:tjsonobject;//JSON classI:integer;//Loop VariableTempstring;//temporary use of variablesJsonarray:tjsonarray;//JSON array VariablesbeginifTrim (Memo1.text) ="'  ThenbeginShowMessage ('to parse the data cannot be empty!');EndElsebeginJsonobject:=Nil;Try{generating JSON from a string}Jsonobject:= Tjsonobject.parsejsonvalue (Trim (Memo1.text)) asTjsonobject;ifJsonobject.count >0  Thenbegin{1, traversing JSON data}Memo2.Lines.Add ('To traverse JSON data:'+ # -#Ten); MEMO2.LINES.ADD ('JSON data quantity:'+IntToStr (Jsonobject.count)); forI: =0  toJsonobject.count-1  Dobeginifi =0  ThenbeginTemp:= Jsonobject.get (i). ToString + # -#Ten;;EndElsebeginTemp:= temp + jsonobject.get (i). ToString + # -#Ten;End;End;{output the JSON to console as String}MEMO2.LINES.ADD (temp); MEMO2.LINES.ADD ('------------------------------');{2, parse JSON data by element}Memo2.Lines.Add ('parse JSON data by element:'+ # -#Ten); Temp:='name ='+ jsonobject.values['name']. ToString + # -#Ten; MEMO2.LINES.ADD (temp);//JSON ArrayJsonarray: = Tjsonarray (Jsonobject.getvalue (' Other'));;ifJsonarray.count >0  Thenbegin//get the JSON array stringTemp: ='Other ='+ Jsonobject.getvalue (' Other'). ToString + # -#Ten;//loops through each element in the JSON array forI: =0  toJsonarray.size-1  DobeginTemp:= temp + INTTOSTR (i +1) +' : '+Jsonarray.items[i]. Value+ # -#Ten;End;End; MEMO2.LINES.ADD (temp);EndElsebeginTemp:='No data!'; MEMO2.LINES.ADD (temp);End;finallyJsonobject.free;End;End;End;//Clear Display DataprocedureTform1.button2click (sender:tobject);beginMemo1.text:="'; Memo2.text:="';End;//set the JSON data to parseprocedureTform1.button3click (sender:tobject);beginMemo1.text:=jsonstring;End;//set the JSON data to parseproceduretform1.formcreate (sender:tobject);beginMemo1.text:=jsonstring;End;proceduretform1.formresize (sender:tobject);begin//reset ButtonSelf . Resetbutton;End;//reset ButtonprocedureTform1.resetbutton;varButtonwidth:integer;beginButtonwidth:= self. WidthDiv 3; Button1.width:=Buttonwidth; Button2.width:=Buttonwidth; Button3.width:=Buttonwidth;End;End.

Source code Download: HTTP://DL5. csdn. Net/fd.php?i=942981887415230&s=8613165e1e05b7e0f273b3a4729e2d0b

Using JSON in Delphi XE7

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.