0
이 JSON 데이터를 Dictionary
으로 변환하려고하는데이 JSON에서 데이터가 어떻게 구성되는지 모르겠습니다. 내 코드 JSON 구조를 잘못 생각하는 것 같아요.JSON 데이터를 사전으로 변환하십시오.
JSON 응답 내 코드 그게 전부
[
{
"userId": 1,
"id": 1,
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
},
{
"userId": 2,
"id": 2,
"title": "et ea vero quia laudantium autem",
"body": "delectus reiciendis molestiae occaecati non minima eveniet qui voluptatibus\naccusamus in eum beatae sit\nvel qui neque voluptates ut commodi qui incidunt\nut animi commodi"
}
]
: 당신이 배열에서 객체에 액세스 할 수 있도록
enum postResult {
case Success([Post]) //return array of post
case Failure(ErrorType)
//
func postsFromJSONData(data:NSData)->PhotosResult{
do {
let jsonObject : AnyObject = try NSJSONSerialization.JSONObjectWithData(data,options:[])
guard let jsonDictionary = jsonObject as? [NSObject:AnyObject],
postsArray = ["userId"] as? [[String:AnyObject]] else {
return.Failure(InvalidError.InvalidJSONData)
}
var finalPosts = [post]()
return.Success(finalPosts)
}
catch let error {
return.Failure(error)
}
}
}
사전을 얻으려면 json 데이터 배열을 먼저 가져와야합니다. 귀하의 json 데이터 'http://www.jsoneditoronline.org/'사이트에서 첫 번째 0 색인 사전에 배열을 확인하십시오. –
당신은 멋진 json persing 연습을 위해 SwiftyJSON을 사용할 수 있습니다. :) – Tuhin