4

需要解析以下 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 (可能不是完全错误,但不是故意的)

4

1 回答 1

6

感谢 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;
于 2013-08-04T08:17:44.920 回答