json - 在Delphi中解析包含另一个jsonstring的字符串数组的JSON字符串
问题描述
所以我正在和这个网络服务器交谈,它返回给我一个像这样的 json 条目:
{
"result": [
[],
["{\"success\": \"true\", \"Message\":\"User 1 has been deleted.\"}"]
]
}
{"result":[[],["{\"success\": \"true\", \"Message\":\"User 1 has been deleted.\"}"]]}
而且我很难从中解决问题。看了一下,我做了一个jsonobject,把result的值拉出来做成一个数组,然后把第一个数组的第二个条目拉成另一个数组,然后把那个jsonstring变成另一个jsonarray,然后把值取出来。但由于某种原因,第一个 jsonarray 声称有两个值,这两个值都是空的。我敢肯定,在我的方法中还有其他错误。
我可以帮我熨一下吗?
procedure DeleteUser;
var
aJSON, aResult : String;
aJsonResponse : TJsonObject;
aResultArrayA : TJsonArray;
aResultArrayB : TJsonArray;
aResultArrayC : TJsonArray;
aParsed : TJsonValue;
i : Integer;
Begin
aresult := '{"result":[[],["{\"success\": \"true\", \"Message\":\"User 1 has been deleted.\"}"]]}';
aJsonResponse := TJsonObject.ParseJSONValue(aResult) as TJsonObject;
if not (aJsonResponse is TJsonObject) then
raise Exception.Create('InvalidResponse');
aResultArrayA := aJsonResponse.getValue('result') as TJsonArray;
if aResultArrayA.Count <= 0 then //is 2
raise Exception.Create('InvalidResponse');
aJSON := aResultArrayA.Items[0].Value; // is ''
aJSON := aResultArrayA.Items[1].Value; // is ''
aResultArrayB := aResultArrayA.Items[0] as TJSONArray;
if aResultArrayB.Count <= 0 then // is 0
raise Exception.Create('InvalidResponse'); //raises here
aJSON := aResultArrayB.Items[0].Value;
aJSON := aResultArrayB.Items[1].Value;
aResultArrayC := TJSONObject.ParseJSONValue(aResultArrayB.Items[1].Value) as TJSONArray;
for aParsed in aResultArrayC do begin
aJson := aJson + aParsed.GetValue<string>('success') + ' ';
aJson := aJson + aParsed.GetValue<string>('message') + ' ';
end;
end;
谢谢大家。
解决方案
我真的认为使用 JSON 的最佳方式是序列化和反序列化。是的,在某些情况下最好使用解析,但请看这个:
uses ...,Rest.Json;
TMyArray = ARRAY of ARRAY of string;
//class for deserialization outer JSON object
TMyParse = CLASS
private
FResult:TMyArray;
procedure SetResult(const Value: TMyArray);
public
property result:TMyArray read FResult write SetResult;
END;
//class for deserialization inner JSON object
TMyInnerParse = class
private
FSuccess:Boolean;
FMessage:string;
procedure SetMessage(const Value: String);
procedure SetSuccess(const Value: Boolean);
public
property success:Boolean read FSuccess write SetSuccess;
property message:String read FMessage write SetMessage;
end;
procedure DeleteUser;
var
OuterObj: TMyParse;
InnerObj: TMyInnerParse;
aResult: String;
i,j: Integer;
Begin
aResult := '{"result":[[],["{\"success\": \"true\", \"Message\":\"User 1 has been deleted.\"}"]]}';
try
OuterObj := TJson.JsonToObject<TMyParse>(aResult);
if Length(OuterObj.result) > 0 then
for i := 0 to Length(OuterObj.result) - 1 do
if length(OuterObj.result[i]) > 0 then
for j := 0 to Length(OuterObj.result[i]) - 1 do
begin
try
InnerObj := TJson.JsonToObject<TMyInnerParse>(OuterObj.result[i][j]);
//Do your work with result, that in InnerObj now
finally
if assigned(InnerObj) then
FreeAndNil(InnerObj);
end;
end;
finally
if assigned(OuterObj) then
FreeAndNil(OuterObj);
end;
end;
procedure TMyParse.SetResult(const Value: TMyArray);
begin
FResult := value;
end;
procedure TMyInnerParse.SetMessage(const Value: String);
begin
FMessage := value;
end;
procedure TMyInnerParse.SetSuccess(const Value: Boolean);
begin
FSuccess := value;
end;
For
这段代码中的循环很糟糕,但它是展示如何解决问题的最快方法。它正在工作。
我不知道第一个空数组中可以包含哪些信息,这可能是异常的原因。将此代码视为工作示例,而不是完整的解决方案,因为缺乏信息。
PS 在这种情况下,使用数组是非常古老的编码方式。但是前段时间我遇到了序列化/反序列化 TList 和 TObjectList 的问题。我会尝试使用它们并返回结果。
PPS 它试图使用 TList,但我的尝试失败了。也许有人可以描述如何在上面的代码中实现它。
推荐阅读
- c# - C# AggregateException,在什么情况下我会聚合异常?
- excel - VBA - 将参数从一个函数传递到另一个函数而不返回第一个函数
- java - 使用 Math.toDegrees 时的代码问题
- python - 优化字梯
- angular - 在 Ionic 4 中,尝试将令牌发送到 Express 路由,但无法验证令牌是否在存储中
- python - 在 txt 文件中查找行号或元素
- javascript - 如何使用 min-max 缩放器对多维二维数组的数据进行归一化
- azure-pipelines - 如何在作业之间共享文件
- java - Scala - 如何在 Spark SQL 查询中将日期字符串转换为时间戳?
- angular - 在 CI-CD azure devops 中发布 angular+.net 核心项目时无法读取未定义的属性“种类”