Ios 将JSON数据转换为字典

Ios 将JSON数据转换为字典,ios,json,swift,nsdictionary,Ios,Json,Swift,Nsdictionary,我试图将此JSON数据转换为字典,但我不知道此JSON中的数据将如何结构化。我认为我的detect JSON结构代码是错误的 JSON响应 [ { "userId": 1, "id": 1, "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit", "body": "quia et suscipit\nsusc

我试图将此JSON数据转换为
字典
,但我不知道此JSON中的数据将如何结构化。我认为我的detect 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)
    //
    f​u​n​c​​ p​osts​F​r​o​m​J​S​O​N​D​a​t​a​(​d​a​t​a​:​​N​S​D​a​t​a​)​​-​>​​P​h​o​t​o​s​R​e​s​u​l​t​​{
        ​​​​d​o ​​{
            ​​​​​​​​l​e​t​​ j​s​o​n​O​b​j​e​c​t​ :​​ A​n​y​O​b​j​e​c​t ​​​​​​​​​​​​​​​​=​​ t​r​y​​ N​S​J​S​O​N​S​e​r​i​a​l​i​z​a​t​i​o​n​.​J​S​O​N​O​b​j​e​c​t​W​i​t​h​D​a​t​a​(​d​a​t​a​,​​o​p​t​i​o​n​s​:​​[​]​)
            ​​​​​​​​g​u​a​r​d​​ l​e​t ​​​​​​​​​​​​j​s​o​n​D​i​c​t​i​o​n​a​r​y​​ =​​ j​s​o​n​O​b​j​e​c​t​​ a​s​?​​ [​N​S​O​b​j​e​c​t​:​A​n​y​O​b​j​e​c​t​]​,
            ​​​​​​​​​​​​p​osts​A​r​r​a​y​​ =​​ [​"​userId"​]​​ a​s​?​​ [​[​S​t​r​i​n​g​:​A​n​y​O​b​j​e​c​t​]​] e​l​s​e​​ {
                r​e​t​u​r​n​​.​F​a​i​l​u​r​e​(​InvalidE​r​r​o​r​.​I​n​v​a​l​i​d​J​S​O​N​D​a​t​a​)
            ​​​​​​​​}

            ​​​​​​​​v​a​r​​ f​i​n​a​l​P​osts​​ =​​ [​post]​(​)
            ​​​​​​​​r​e​t​u​r​n​​.​S​u​c​c​e​s​s​(​f​i​n​a​l​P​osts​)
    ​​​​    }
        ​​​​c​a​t​c​h l​e​t​​ e​r​r​o​r​​ {
        ​​​​​​​​    r​e​t​u​r​n​​.​F​a​i​l​u​r​e​(​e​r​r​o​r​)
        ​​​​}
    }
}

您的响应是数组而不是字典,所以您需要从数组访问对象,并且您的
用户ID
键包含数字作为值而不是
数组/字典

l​e​t​​ j​s​o​n​O​b​j​e​c​t​ ​​​​​​​​​​​​​​​​=​​ t​r​y​​N​S​J​S​O​N​S​e​r​i​a​l​i​z​a​t​i​o​n​.​J​S​O​N​O​b​j​e​c​t​W​i​t​h​D​a​t​a​(​d​a​t​a​,​​o​p​t​i​o​n​s​:​​[​]​)​ as! [[String: AnyObject]]
if j​s​o​n​O​b​j​e​c​t​.count > 0 {
    if let userId = j​s​o​n​O​b​j​e​c​t​[0]["userId"] as? Int {
         print(userId)
    } 
}

您的响应是数组而不是字典,所以您需要从数组访问对象,并且您的
用户ID
键包含数字作为值而不是
数组/字典

l​e​t​​ j​s​o​n​O​b​j​e​c​t​ ​​​​​​​​​​​​​​​​=​​ t​r​y​​N​S​J​S​O​N​S​e​r​i​a​l​i​z​a​t​i​o​n​.​J​S​O​N​O​b​j​e​c​t​W​i​t​h​D​a​t​a​(​d​a​t​a​,​​o​p​t​i​o​n​s​:​​[​]​)​ as! [[String: AnyObject]]
if j​s​o​n​O​b​j​e​c​t​.count > 0 {
    if let userId = j​s​o​n​O​b​j​e​c​t​[0]["userId"] as? Int {
         print(userId)
    } 
}

首先获取json数据数组,然后获取字典。请检查您的json数据“”站点字典后数组中的第一个0索引。您可以使用SwiftyJSON进行良好的json发布实践…:)在获取字典后,您将首先获取json数据数组。请检查您的json数据“”站点字典后数组中的第一个0索引。您可以使用SwiftyJSON进行出色的json发布实践…:)