{
"Children" : [
{
"Code" : "YJ0804,"
"HavePicture" : 0,
"Id" : "134389",
"Name" : "crystal photo albums,"
"SameNegative" : false
},
{
"Code" : "XKo03,"
"HavePicture" : 0,
"Id" : "134388",
"Name" : "10" ou dian pendulum box ",
"SameNegative" : true
}
],
"Id" : "eee19ba6dfd648a68a2bcea6d16f51b5,"
"MaxPicture" : 0,
"Name" : "2018 growth set is a",
"Open" : true,
"Root" : true
},
{
"Children" : [
{
"Code" : "CP00000229,"
"HavePicture" : 0,
"Id" : "134588",
"MaxPicture" : 1,
"Name" : "8 inch hermes party photo album,"
"ProductType" : "album,"
"SameNegative" : false
}
],
"Id", "secondary consumption",
"MaxPicture" : 0,
"Name" : "secondary consumption,"
"Open" : true,
"Root" : true
}
]
CodePudding user response:
Ah how to read how to read, normal array array is the first layer, layer 2 is object, the children inside the element type is an array, and then is the key - the value in itCodePudding user response:
First converted to a json object, D7 in superobject pas, high version of the Delphi can directly use System. JsonCodePudding user response:
Use the superobject pas, according to the node's model, for reading and writing,CodePudding user response:
VarJsonObj, Item, ItemChild: ISuperObject;
The begin
JsonObj:=SO (your json string);
For the Item in jsonObj do begin
Item. S [' id '];
The Item. I [' maxPicture];
.
For ItemChild Item in the do the begin
Item. S (' code ');
The Item. I [' havePicture];
.
end;
end;
end;
CodePudding user response:
Delphi has a built-in System. Latest json. Serializers unit can be directly to json format serialization and deserialization Deserialize, Initialize. You can check the informationCodePudding user response:
The function TCommonFileS. The Initialize (AJSONString: WideString; AKey: String): Boolean;
Var
I: integer;
JSONObject: TJSONObject;
JSONArray: TJSONArray;
JSONCommonFile: TJSONCommonFile;
The begin
JSONObject:=nil;
Result:=True;
Try
//TJSONObject ParseJSONValue exception happens
JSONObject:=TJSONObject ParseJSONValue (AJSONString) as TJSONObject;
If JSONObject. TryGetValue (AKey, JSONArray) then
The begin
If Assigned (JSONArray) then
The begin
The Self. The Clear;
For I:=0 to JSONArray. Do the Count - 1
The begin
JSONCommonFile:=JSONArray. Items [I] ToJSON;
The Self. The Add (TCommonFile. Deserialize (JSONCommonFile));
end;
end;
end;
Except,
On E: the Exception do
The begin
Result:=False;
ShowMessage (Format (' @ % s.I nitialize=& gt; "% s" % s', [the ClassName, AJSONString,
E.m essage]));
end;
end;
JSONObject. Free;
end;
CodePudding user response:
This is a converting json into an object, to convert an object to a json example you can according to the format toThe unit Unit14;
Interface
USES the
Winapi. Windows, Winapi Messages, System. SysUtils, System. Variants,
System. The Classes, the Vcl. Graphics,
The Vcl. Controls, Vcl. Forms, Vcl. Dialogs, Vcl. StdCtrls;
Type
TForm14=class (TForm)
For: TButton;
Memo1: TMemo;
Button2: TButton;
Memo2: TMemo;
Procedure Button1Click (Sender: TObject);
Procedure Button2Click (Sender: TObject);
Private
{Private declarations}
Public
{Public declarations}
end;
TUser=class
Public
FAge: Integer;
FLastName: string;
FFirstName: string;
end;
Var
Form14: TForm14;
Implementation
USES the
System. Json,
System. The json. Converters,
System. The json Serializers,
System. Generics. Collections;
{$R *. DFM}
Procedure TForm14. Button1Click (Sender: TObject);
Var
User: TUser;
The userList: TObjectList
//userList: TArray
JSONObject: TJSONObject;
The begin
//SetLength (userList, 2);
The userList:=TObjectList
User:=TUser. Create;
User. FAge:=1;
User. FFirstName:='Delphi';
User. FLastName:='English';
//userList [0] :=user;
//userList. Add (user);
User:=TUser. Create;
User. FFirstName:='Chinese';
User. FAge:=2;
User. FLastName:='Tokyo';
//userList. Add (user);
//userList [1] :=user;
Memo1. Text:=TJsonSerializer. Create. Serialize (userList);
end;
Procedure TForm14. Button2Click (Sender: TObject);
Var
//userList: TArray
The userList: TObjectList
User: TUser;
The begin
The userList:=TJsonSerializer. Create. Deserialize & lt; TObjectList & lt; TUser & gt;>
(Memo1. Text);
Memo2. The Clear;
For the user in the userList do
The begin
Memo2. Lines. The Add user. FAge. ToString ();
Memo2. Lines. The Add (user. FFirstName);
Memo2. Lines. The Add (user. FLastName);
end;
{
User:=TJsonSerializer. Create. Deserialize
Memo2. Lines. The Add user. FAge. ToString ();
Memo2. Lines. The Add (user. FFirstName);
nullnullnullnull