类似这种 JSON 对象数组 格式如何读取?

Danny1123 2018-12-11 05:02:47
类似这种json数组格式如何读取?
[
{
"children": [
{
"code": "YJ0804",
"havePicture": 0,
"id": "134389",
"name": "晶相册",
"sameNegative": false
},
{
"code": "XKo03",
"havePicture": 0,
"id": "134388",
"name": "10寸欧典摆框",
"sameNegative": true
}
],
"id": "eee19ba6dfd648a68a2bcea6d16f51b5",
"maxPicture": 0,
"name": "2018成长套系一",
"open": true,
"root": true
},
{
"children": [
{
"code": "CP00000229",
"havePicture": 0,
"id": "134588",
"maxPicture": 1,
"name": "方8寸爱马仕相册",
"productType": "相册",
"sameNegative": false
}
],
"id": "二次消费",
"maxPicture": 0,
"name": "二次消费",
"open": true,
"root": true
}
]
...全文
1125 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
别激动 2019-01-25
  • 打赏
  • 举报
回复
这是一个将json转换成一个对象、将一个对象转换成json的例子你可以按着这个格式来 unit Unit14; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls; type TForm14 = class(TForm) Button1: 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 System.json, System.json.Converters, System.json.Serializers, System.Generics.Collections; {$R *.dfm} procedure TForm14.Button1Click(Sender: TObject); var user: TUser; userList: TObjectList<TUser>; // userList: TArray<TUser>; JSONObject: TJSONObject; begin // SetLength(userList, 2); userList := TObjectList<TUser>.Create; user := TUser.Create; user.FAge := 1; user.FFirstName := 'delphi'; user.FLastName := '英文'; // userList[0] := user; // userList.Add(user); user := TUser.Create; user.FFirstName := '中文'; 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<TUser>; userList: TObjectList<TUser>; user: TUser; begin userList := TJsonSerializer.Create.Deserialize < TObjectList < TUser >> (Memo1.Text); Memo2.Clear; for user in userList do begin Memo2.Lines.Add(user.FAge.ToString); Memo2.Lines.Add(user.FFirstName); Memo2.Lines.Add(user.FLastName); end; { user := TJsonSerializer.Create.Deserialize<TUser>(Memo1.Text); Memo2.Lines.Add(user.FAge.ToString); Memo2.Lines.Add(user.FFirstName); Memo2.Lines.Add(user.FLastName); } end; end.
别激动 2019-01-25
  • 打赏
  • 举报
回复
function TCommonFileS.Initialize(AJSONString: WideString; AKey: String) : Boolean; var I: integer; JSONObject: TJSONObject; JSONArray: TJSONArray; JSONCommonFile: TJSONCommonFile; begin JSONObject := nil; Result := True; try // TJSONObject.ParseJSONValue会发生异常 JSONObject := TJSONObject.ParseJSONValue(AJSONString) as TJSONObject; if JSONObject.TryGetValue(AKey, JSONArray) then begin if Assigned(JSONArray) then begin Self.Clear; for I := 0 to JSONArray.Count - 1 do begin JSONCommonFile := JSONArray.Items[I].ToJSON; Self.Add(TCommonFile.Deserialize(JSONCommonFile)); end; end; end; except on E: Exception do begin Result := False; ShowMessage(Format('@%s.Initialize=> "%s" %s', [ClassName, AJSONString, E.message])); end; end; JSONObject.Free; end;
别激动 2019-01-25
  • 打赏
  • 举报
回复
delphi 最新有自带的System.json.Serializers单元可以对json格式直接序列化与反序列化Deserialize,Initialize;你可以查这方面资料
lyhoo163 2018-12-16
  • 打赏
  • 举报
回复
使用superobject.pas,按节点的模式,进行读写。
chenjun_0312 2018-12-16
  • 打赏
  • 举报
回复
var
jsonObj,Item,ItemChild : ISuperObject;

begin
jsonObj := SO(你的json字符串);
for Item in jsonObj do begin
Item.S['id'];
Item.I['maxPicture'];
.....
for ItemChild in Item do begin
Item.S['code'];
Item.I['havePicture'];
.....
end;
end;
end;
无聊的猪 2018-12-14
  • 打赏
  • 举报
回复
先转换成json对象,D7用superobject.pas,高版本的delphi可以直接用System.JSON
  • 打赏
  • 举报
回复
正常数组啊 该怎么读就怎么读, 第一层是array, 第2层是object, 里面children元素类型又是array, 然后里面就是key-value

1,593

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 网络通信/分布式开发
社区管理员
  • 网络通信/分布式开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧