2013-08-03 81 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

不,很抱歉 –