gpt4 book ai didi

json - Delphi解析JSON数组或数组

转载 作者:行者123 更新时间:2023-12-03 09:59:34 25 4
gpt4 key购买 nike

这是我希望能够解析的示例 JSON:

[
{
"a":{
"username":"aaa",
"email":"aaa@gmail.com"
}
},
{
"b":{
"username":"bbb",
"email":"bbb@gmail.com"
}
}
]

我需要调用 getData('b', 'email')必须输出 bbb@gmail.com !

我真的很难理解如何使用 System.JSON单位,但我无法得到解决方案!我希望能够编写一个从上述 JSON 结构中提取特定数据的函数。到目前为止,这是我的代码。在类构造函数中,我有:

var
FIds: TJSONArray;
begin
FIds := TJSONObject.ParseJSONValue({json string here}) as TJSONArray;
end;

然后,在必须返回数据的函数中,我写了这个:

// 'name' can be 'a' or 'b'  | 'data' can be 'username' or 'email'
function TTest.getData(const name, data: string): string;
var
FValue, FValueInner: TJSONValue;
begin
for FValue in Fids do
begin
if (FValue is TJSONArray) then
begin
//Here I am sure that I have a TJSONArray (which can be 'a' or 'b' from above)
end;
end;
end;

根据我上面写的,我必须检查 name的值并决定是否必须访问 a 中的数据或 b .然后,一旦我选择了正确的 JSON 数组 ab , 我必须选择是否要显示 usernameemail字段(在 data 变量中指定)。

我怎样才能做到这一点?

这是我最近的尝试,但我真的不明白该怎么做:

... same code above ...

if (FValue is TJSONArray) then
begin
//here I want to check if the current array is a or b
if ((FValue as TJSONArray).Items[0] as TJSONValue).Value = name then
begin
Farr := TJSONObject.ParseJSONValue(((FValue as TJSONArray).Items[0] as TJSONValue).ToJSON) as TJSONArray;
try
//here I want to get the data inside username or email
for FValueInner in Farr do
Result := FValueInner.GetValue<string>(data);
finally
Farr.Free;
end;
end;
end;

哪里 Farr: TJSONArray;FValueInner: TJSONValue;

最佳答案

对于寻找这些答案的新读者。

这个函数怎么样,或者如果你重组 JSON 数据甚至更简单?

function getData(JsonString: String; User: String; Field: String): String;
var
JSonValue: TJSonValue;
JsonArray: TJSONArray;
ArrayElement: TJSonValue;
FoundValue: TJSonValue;
begin
Result :='';

// create TJSonObject from string
JsonValue := TJSonObject.ParseJSONValue(JsonString);

// get the array
JsonArray := JsonValue as TJSONArray;

// iterate the array
for ArrayElement in JsonArray do begin
FoundValue := ArrayElement.FindValue(User);
if FoundValue <> nil then begin
Result := ArrayElement.GetValue<string>(User + '.' + Field);
break;
end;
end;
end;

上面示例 JSON 代码的问题在于它使用用户名“a”“b”作为用户数据的 JSON 键 {key:data}。这样您就不能在搜索数据时使用 GetValue("a") 。以不同方式构建 JSON 数据可以使搜索过程更加轻松。我稍后会举一个例子。

处理给定 JSON 数据的一种方法是使用 FindValue 所以你可以检查一个字段是否带有 key “a”或“b”存在。
FoundValue := ArrayElement.FindValue("b");
if FoundValue <> nil then begin
Result := ArrayElement.GetValue<string>("b"+ '.' + "email");
break;

关于“解析 JSON 数组”问题:将数据作为 TJSonObject 加载后,您可以将数据更改为 TJSONArray 并迭代元素。
  JsonValue := TJSonObject.ParseJSONValue(JsonString);  
JsonArray := JsonValue as TJSONArray;
for ArrayElement in JsonArray do begin
...

给定 JSON 数据的工作示例代码:
unit JsonArray1;

interface

uses System.JSON;

function getData2(JsonString: String; User: String; Field: String): String;
procedure Test1();

implementation

function getData2(JsonString: String; User: String; Field: String): String;
var
JSonValue: TJSonValue;
JsonArray: TJSONArray;
ArrayElement: TJSonValue;
FoundValue: TJSonValue;
begin
Result :='';

// create TJSonObject from string
JsonValue := TJSonObject.ParseJSONValue(JsonString);

// get the array
JsonArray := JsonValue as TJSONArray;

// iterate the array
for ArrayElement in JsonArray do begin
FoundValue := ArrayElement.FindValue(User);
if FoundValue <> nil then begin
Result := ArrayElement.GetValue<string>(User + '.' + Field);
break;
end;
end;
end;

procedure Test1();
var
DataBase: String;
EmailAddress : String;
Username: String;
begin
DataBase := '[ {"a" : {"username":"aaa","email":"aaa@gmail.com"}},' +
'{"b" : {"username":"bbb","email":"bbb@gmail.com"}} ]';

EmailAddress := getData2(DataBase, 'b', 'email');
Username := getData2(DataBase, 'a', 'username');

end;

end.

如前所述,使用适当的键重构 JSON 数据使查找数据的代码更加简单。因为用户数据 "a":{}, "b":{} 之间存在 1 对 1 关系,所以很容易引入“用户”键。还将“用户”键添加到数组会导致所有数据都具有键。
  '{"users" : [{ "user":"a", "username":"aaa","email":"aaa@gmail.com"},' +
'{ "user":"b", "username":"bbb","email":"bbb@gmail.com"}]}';

当您迭代用户时,您现在可以使用带有新“用户”键的 GetValue。
  if ArrayElement.GetValue<String>('user') = 'b' then begin
Result := ArrayElement.GetValue<String>('email');

通过给数组一个键,你现在可以得到数组:
JsonArray := JsonValue.GetValue<TJSONArray>('users');

重组 JSON 数据的工作示例代码:
unit JsonArray2;

interface

uses System.JSON;

function getData2(JsonString: String; User: String; Field: String): String;
procedure Test2();

implementation

function getData2(JsonString: String; User: String; Field: String): String;
var
JSonValue: TJSonValue;
JsonArray: TJSONArray;
ArrayElement: TJSonValue;
FoundValue: TJSonValue;
begin
Result :='';

// create TJSonObject from string
JsonValue := TJSonObject.ParseJSONValue(JsonString);

// get the array
JsonArray := JsonValue.GetValue<TJSONArray>('users');

// iterate the array
for ArrayElement in JsonArray do begin
if ArrayElement.GetValue<String>('user') = User then begin
Result := ArrayElement.GetValue<String>(Field);
break;
end;
end;
end;

procedure Test2();
var
DataBase: String;
EmailAddress : String;
Username: String;
begin
DataBase := '{"users" : [{ "user":"a", "username":"aaa","email":"aaa@gmail.com"},' +
'{ "user":"b", "username":"bbb","email":"bbb@gmail.com"}]}';

EmailAddress := getData2(DataBase, 'b', 'email');
Username := getData2(DataBase, 'a', 'username');

end;

end.

关于json - Delphi解析JSON数组或数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46534011/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com