delphi中Json的处理
【摘要】 悠悠岁月 欲说当年好困惑亦真亦幻难取舍悲欢离合都曾经有过这样执着究竟为什么漫漫人生路 上下求索心中渴望真诚的生活谁能告诉我是对还是错...当我们要处理Json时,一般情况分两个步骤,一是生成Json,二是解析,因superObject Json是无序,所以我们这里使用二次封装System.JSON的单元:JsonDataObjects.pas(见附件),推荐使用原因:Json节点有序性(有序...
悠悠岁月 欲说当年好困惑
亦真亦幻难取舍
悲欢离合都曾经有过
这样执着究竟为什么
漫漫人生路 上下求索
心中渴望真诚的生活
谁能告诉我是对还是错...
当我们要处理Json时,一般情况分两个步骤,一是生成Json,二是解析,因superObject Json是无序,所以我们这里使用二次封装System.JSON的单元:JsonDataObjects.pas(见附件,不支持.pas,直接存成文本),推荐使用原因:Json节点有序性(有序性在数据传输或者验证数据时是必要的)、自释放节点管理
一、我们要生成一个Json,格式示例如下:
{
"mainData": {
"schollName": "五道口职业技术学院",
"teacherNamesArray": [
"张老师",
"李老师"
],
"teacherDataArray": [
{
"name": "张老师",
"sex": "女",
"age": 18,
"salary": 2800.56,
"onBoarDate": "2023-12-05"
},
{
"name": "李老师",
"sex": "女",
"age": 18,
"salary": 2800.56,
"onBoarDate": "2022-12-05"
}
],
"studentDataArray": [
{
"studentOverallScore": 523.5,
"studentData": {
"studentName": "Kitty",
"studentSex": "女",
"studentNo": "17"
}
},
{
"studentOverallScore": 665,
"studentData": {
"studentName": "韩梅梅",
"studentSex": "女",
"studentNo": "17"
}
}
]
}
}
二、生成这个JSON,代码段如下:
function TForm1.GenerateJson : string;
var
AMainJson : TJsonObject;
AteacherNamesArray, AteacherDataArray,AstudentDataArray : TJsonArray;
AteacherDataJson,AstudentJson,AstudentDataJson : TJsonObject;
begin
AMainJson := TJsonObject.Create;
AMainJson.S['schollName'] := '五道口职业技术学院';
AteacherNamesArray := TJsonArray.Create;
AteacherNamesArray.Add('张老师');
AteacherNamesArray.Add('李老师');
AMainJson.A['teacherNamesArray'] := AteacherNamesArray;
AteacherDataArray := TJsonArray.Create;
AteacherDataJson := TJsonObject.Create;
AteacherDataJson.S['name'] := '张老师';
AteacherDataJson.S['sex'] := '女';
AteacherDataJson.I['age'] := 18;
AteacherDataJson.F['salary'] := 2800.56; //F是Double,D是DateTime
AteacherDataJson.S['onBoarDate'] := '2023-12-05';
AteacherDataArray.AddObject(AteacherDataJson);
AteacherDataJson := TJsonObject.Create;
AteacherDataJson.S['name'] := '李老师';
AteacherDataJson.S['sex'] := '女';
AteacherDataJson.I['age'] := 18;
AteacherDataJson.F['salary'] := 2800.56; //F是Double,D是DateTime
AteacherDataJson.S['onBoarDate'] := '2023-12-05';
AteacherDataArray.AddObject(AteacherDataJson);
AMainJson.A['teacherDataArray'] := AteacherDataArray;
AstudentDataArray := TJsonArray.Create;
AstudentJson := TJsonObject.Create;
AstudentJson.F['studentOverallScore'] := 523.5;
AstudentDataJson := TJsonObject.Create;
AstudentDataJson.S['studentName'] := 'Kitty';
AstudentDataJson.S['studentSex'] := '女';
AstudentDataJson.I['studentNo'] := 17;
AstudentJson.O['studentData'] := AstudentDataJson;
AstudentDataArray.AddObject(AstudentJson);
AstudentJson := TJsonObject.Create;
AstudentJson.F['studentOverallScore'] := 665;
AstudentDataJson := TJsonObject.Create;
AstudentDataJson.S['studentName'] := '韩梅梅"';
AstudentDataJson.S['studentSex'] := '女';
AstudentDataJson.I['studentNo'] := 17;
AstudentJson.O['studentData'] := AstudentDataJson;
AstudentDataArray.AddObject(AstudentJson);
AMainJson.A['studentDataArray'] := AstudentDataArray;
Result := AMainJson.ToString;
Memo1.Lines.Add(Result);
AMainJson.Free; //托管式节点对象释放
end;
三、解析这个JSON,代码段如下:
procedure TForm1.Button1Click(Sender: TObject);
var
AresultStr : string;
AMainJson : TJsonObject;
AteacherNamesArray, AteacherDataArray,AstudentDataArray : TJsonArray;
i : Integer;
begin
AresultStr := GenerateJson;
AMainJson := TJsonObject.Parse(AresultStr) as TJsonObject;
AteacherNamesArray := AMainJson.A['teacherNamesArray'];
AteacherDataArray := AMainJson.A['teacherDataArray'];
AstudentDataArray := AMainJson.A['studentDataArray'];
for I := 0 to AteacherNamesArray.Count - 1 do
begin
//两种方式
Memo1.Lines.Add(AteacherNamesArray[i]);
Memo1.Lines.Add(AteacherNamesArray[i].Value);
end;
for I := 0 to AstudentDataArray.Count - 1 do
begin
Memo1.Lines.Add(AstudentDataArray[i].O['studentData'].S['studentName']);
end;
AMainJson.Free;
end;
四、补充
1、如果遇到Json串中有很多"\","\\","\\\\",可能会遇到解析不成TJsonObject时,先把他取出来赋值给string,然后再解析这个string
2、平时如果遇到Free报错时,要注意:.O,.A是指针,不要重复Free或FreeAndNil
3、超大Json进度要加上TJsonReaderProgressRec,这个参数,可以查看进度
【版权声明】本文为华为云社区用户原创内容,未经允许不得转载,如需转载请自行联系原作者进行授权。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)