2016-06-10 60 views
1

我一直在這一段時間,但無法得到解析正義。我試圖解析這個JSON文件:用SwiftyJSON解析嵌入的JSON

{ 
"order_history" : [ { 
    "items" : [ { 
    "id" : 284, 
    "created" : [ 2016, 5, 26, 5, 27, 53 ], 
    "updated" : [ 2016, 5, 27, 0, 31, 10 ], 
    "sku" : "10-10-08-050", 
    "name" : "Product one of set one", 
    "description" : "", 
    "quantity" : 1.0, 
    "price" : 2000.0, 
    "total" : 2000.0, 
    "tax" : null, 
    "discount" : null 
}, { 
    "id" : 285, 
    "created" : [ 2016, 5, 26, 5, 27, 53 ], 
    "updated" : [ 2016, 5, 27, 0, 31, 10 ], 
    "sku" : "10-22-12-247", 
    "name" : "Product 2 of set 1", 
    "description" : "", 
    "quantity" : 1.0, 
    "price" : 2300.0, 
    "total" : 2300.0, 
    "tax" : null, 
    "discount" : null 
}, { 
    "id" : 286, 
    "created" : [ 2016, 5, 26, 5, 27, 53 ], 
    "updated" : [ 2016, 5, 27, 0, 31, 10 ], 
    "sku" : "10-22-12-249", 
    "name" : "Product 3 of set 1", 
    "description" : "", 
    "quantity" : 1.0, 
    "price" : 3700.0, 
    "total" : 3700.0, 
    "tax" : null, 
    "discount" : null 
} ], 

"items" : [ { 
    "id" : 288, 
    "created" : [ 2016, 5, 26, 5, 29, 51 ], 
    "updated" : [ 2016, 5, 27, 0, 31, 11 ], 
    "sku" : "JJ-02-00-042", 
    "name" : "Product 1 of set 2", 
    "description" : "", 
    "quantity" : 1.0, 
    "price" : 3000.0, 
    "total" : 3000.0, 
    "tax" : null, 
    "discount" : null 
} ], 

"items" : [ { 
    "id" : 310, 
    "created" : [ 2016, 5, 30, 7, 40, 41 ], 
    "updated" : [ 2016, 5, 30, 7, 40, 46 ], 
    "sku" : "J481", 
    "name" : "Product 1 set 3", 
    "description" : "", 
    "quantity" : 1.0, 
    "price" : 2200.0, 
    "total" : 2200.0, 
    "tax" : null, 
    "discount" : null 
} ], 

"items" : [ { 
    "id" : 311, 
    "created" : [ 2016, 5, 30, 7, 48, 39 ], 
    "updated" : [ 2016, 5, 30, 7, 48, 44 ], 
    "sku" : "JJ1", 
    "name" : "Product 2 set 3", 
    "description" : "", 
    "quantity" : 1.0, 
    "price" : 2200.0, 
    "total" : 2200.0, 
    "tax" : null, 
    "discount" : null 
} ], 

"items" : [ { 
    "id" : 312, 
    "created" : [ 2016, 5, 30, 9, 8, 31 ], 
    "updated" : [ 2016, 5, 30, 9, 8, 32 ], 
    "sku" : "J77", 
    "name" : "Product 3 in set 3", 
    "description" : "", 
    "quantity" : 1.0, 
    "price" : 2200.0, 
    "total" : 2200.0, 
    "tax" : null, 
    "discount" : null 
} ] 
} 
] 
} 

因爲它現在是我所能得到的這是第一套產品。我想要做的就是獲得所有三套以及「創建」字段。我根本沒有得到創造的領域。它只是空的。

這是怎麼了,我一直得到的數據出來的JSON文件

 for (_, myosin) in newJson["order_history"][0]["items"] { 
      if let set = myosin["name"].string { 
       //products is a string crated somewhere up there^
       products.appendContentsOf("\n" + name) 
      } 

     } 

的感謝您的任何幫助。

+1

多德存在前兩個項目的字典中沒有名稱字段 –

+0

我知道這就是爲什麼我不認爲這是可能的,但我想我會問。這是我被告知解析的。 – MNM

回答

2

您正在將「名稱」字段解碼爲set變量,但您試圖使用不存在的name變量。您也正在使用appendContentsOf,但它應該是append

var products = [String]() 

for (_, myosin) in newJson["order_history"][0]["items"] { 
    if let set = myosin["name"].string { 
     products.append("\n" + set) 
    } 
} 

print(products) 

而要獲得每個字典中的「創建」數組:

var createdArrays = [[Int]]() 

for (_, myosin) in newJson["order_history"][0]["items"] { 
    if let created = myosin["created"].arrayObject as? [Int] { 
     createdArrays.append(created) 
    } 
} 

print(createdArrays) 
+0

謝謝@Eric D我很感激。我仍然習慣於快速編寫代碼 – MNM

+0

不客氣。 – Moritz