2013-08-03 80 views
3

需要解析以下JSON,但无法弄清楚如何使用D2007和uJSON 有人可以告诉我如何可以访问这个值吗?我如何解析与德尔福JSON的

{ 
    "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": "[email protected]" 
} 

在此先感谢!

编辑:纠正错误的JSON(也许不是完全错误,但不打算)

+4

你可以看看[超对象(https://code.google.com/p/superobject/)和实例来替代uJson –

+4

你的json无效。项目嵌套值是一个字符串,而不是一个有效的JSON数组... –

+1

这不会使它无效JSON,@Arnaud。对于其中一个字符串值*发生*完全可能包含本身可被解释为更多JSON的内容。不过,是否允许JSON字符串包含换行符是另一回事。 –

回答

6

由于入佛门先生,我与超对象尝试过了,我能得到它的工作。 在这里我的解决方案希望能帮助别人。不知道它是最短的方式,但它的工作原理。

但是,如果你可以写一些较短的代码随意编辑这个答案。 (如果你还可以纠正我的英语很差;)

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; 
+0

上次编辑的原因是什么?你还在寻找答案吗?有什么问题?如果您有任何问题可随时发布或更新,但不要期望任何人回答问题 –

+0

不,很抱歉 –