如何在 Delphi 10.3 中解析此 json 数据?
How to parse this json data in Delphi 10.3?
我是 Delphi 的新手,目前正在学习。
我有代码的和平。效果很好。
procedure TForm2.Button1Click(Sender: TObject);
var
jValue : TJSONValue;
JsonArray: TJSONArray;
begin
RESTRequest1.Execute;
jValue := RESTResponse1.JSONValue;
// Ist this the right way? --> JsonArray := TJSonObject.ParseJSONValue(jValue.ToString) as TJSONArray;
MemoContent.Lines.Add('Zitat des Tages:');
MemoContent.Lines.Add(jValue.ToString);
end;
输出如下JSON字符串:
Zitat des Tages:\n
{"951":{"zitat":"\nWo ein Wille ist, ist auch ein Holzweg.\n\n","autor":"André Brie"},"timestamp":"2020 09 03 19:21:36"}
现在,我想解析 JSON 对象以将 zitat、autor 写入我的备忘录。但我不知道该怎么做。我读了很多,但我不明白将我的字符串放入数组然后将其解析为元素。
各位教授有什么建议或帮助吗?
感谢您的帮助。
此代码可能会执行您需要的解析:
uses System.JSON;
procedure TForm1.Button1Click(Sender: TObject);
JSonData : String;
JSonObject : TJSonObject;
JSonValue : TJSonValue;
Zitat : String;
begin
JSonData := '{"951":{"zitat":"\nWo ein Wille ist, ist auch ein Holzweg.\n\n","autor":"André Brie"},"timestamp":"2020 09 03 19:21:36"}';
JSonObject := TJSonObject.ParseJSONValue(JSonData) as TJSonObject;
try
JSonValue := JSonObject.Get('951').JSONValue;
Zitat := JSonValue.GetValue<string>('zitat');
finally
JSonObject.Free;
end;
ShowMessage(Zitat);
end;
您应该添加更多测试来处理 JSON 数据格式错误或不包含所需数据的情况。
我是 Delphi 的新手,目前正在学习。 我有代码的和平。效果很好。
procedure TForm2.Button1Click(Sender: TObject);
var
jValue : TJSONValue;
JsonArray: TJSONArray;
begin
RESTRequest1.Execute;
jValue := RESTResponse1.JSONValue;
// Ist this the right way? --> JsonArray := TJSonObject.ParseJSONValue(jValue.ToString) as TJSONArray;
MemoContent.Lines.Add('Zitat des Tages:');
MemoContent.Lines.Add(jValue.ToString);
end;
输出如下JSON字符串:
Zitat des Tages:\n
{"951":{"zitat":"\nWo ein Wille ist, ist auch ein Holzweg.\n\n","autor":"André Brie"},"timestamp":"2020 09 03 19:21:36"}
现在,我想解析 JSON 对象以将 zitat、autor 写入我的备忘录。但我不知道该怎么做。我读了很多,但我不明白将我的字符串放入数组然后将其解析为元素。
各位教授有什么建议或帮助吗?
感谢您的帮助。
此代码可能会执行您需要的解析:
uses System.JSON;
procedure TForm1.Button1Click(Sender: TObject);
JSonData : String;
JSonObject : TJSonObject;
JSonValue : TJSonValue;
Zitat : String;
begin
JSonData := '{"951":{"zitat":"\nWo ein Wille ist, ist auch ein Holzweg.\n\n","autor":"André Brie"},"timestamp":"2020 09 03 19:21:36"}';
JSonObject := TJSonObject.ParseJSONValue(JSonData) as TJSonObject;
try
JSonValue := JSonObject.Get('951').JSONValue;
Zitat := JSonValue.GetValue<string>('zitat');
finally
JSonObject.Free;
end;
ShowMessage(Zitat);
end;
您应该添加更多测试来处理 JSON 数据格式错误或不包含所需数据的情况。