2016-06-10 4 views
1

저는 잠시 동안 있었지만 구문 분석을 제대로 수행하지 못했습니다. 이 json 파일을 구문 분석하려고합니다.SwifftyJSON을 사용하여 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

"name"필드를 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