gpt4 book ai didi

json - 如何用Delphi解析Json

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

需要解析以下 JSON,但不知道如何使用 D2007 和 uJSON 进行解析有人可以向我展示如何访问这些值吗?

{
"id": "40",
"created_at": "2013-08-02 20:50:28",
"delivery_at": "2013-08-02 20:50:28",
"cid": "7",
"firstname": "Joe",
"lastname": "Average",
"street": "Joes Place",
"items": [
{
"id": 601,
"price": 25,
"name": "Pizza Party 40x60 cm",
"qty": 1,
"opt": 8,
"extras": [
[
"Salmon",
0
],
[
"Spinach",
1.5
],

]
}
],
"eMail": "me@examble.com"
}

提前致谢!

编辑:更正了错误的json(也许不是完全错误,但不是有意的)

最佳答案

感谢 Rufo 爵士,我用 SuperObject 尝试过,我可以让它工作。这是我的解决方案希望对其他人有帮助。不知道这是否是最短的方法,但它有效。

但是如果您可以编写一些较短的代码,请随意编辑此答案。 (如果你能纠正我糟糕的英语;)

var
order, pos: ISuperObject;
firstname, lastname, street, created_at, delivery_at, cid, eMail : String;
id, i : Integer;
begin

order := SO(<jsontext>);

id := order.AsObject.I['id'];
fistname := order.AsObject.S['firstname'];
lastname := order.AsObject.S['lastname'];
street := order.AsObject.S['street'];
cid := order.AsObject.S['cid'];
eMail := order.AsObject.S['eMail'];
created_at := order.AsObject.S['created_at'];
delivery_at := order.AsObject.S['delivery_at'];

// do some stuff with your values
// and next are the articles of our pizza order ;)
for pos in order['items'] do begin

// get the values like this
ShowMessage(pos['name'].AsString)
end;

// and now the array of extra ingredients for an particular article

for i := 0 to pos['extras'].AsArray.Length - 1 do begin

// do some stuff here we Show it again only for demonstration purpose
ShowMessage(pos['extras[' + IntToStr(i) + '][0]'].AsString)
end

end;

关于json - 如何用Delphi解析Json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18032078/

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