2016-08-05 55 views
1

我要解析的json文件没有“结果”选项卡我的新JSON文件下斯威夫特JSON删除键

[ 
    { 
    "Id": 708, 
    "Name": "My name", 
    "ImageUrl": "2016728135316.jpg" 
    } 

代码下

private func getMoviesFromJSON(jsonData: NSData) throws -> [Movie] { 
     var movies = [Movie]() 
     do { 
      if let jsonObject = try NSJSONSerialization.JSONObjectWithData(jsonData, options: .AllowFragments) as? [String: AnyObject], jsonArray = jsonObject["results"] as? [[String: AnyObject]] { 
       for i in jsonArray { 
        var properties = [String: AnyObject]() 
        properties[JSONKeys.id] = i[JSONKeys.id] 
        properties[JSONKeys.title] = i[JSONKeys.title] 
        properties[JSONKeys.posterPath] = i[JSONKeys.posterPath] 
        let movie = Movie(properties: properties) 
        movies.append(movie) 
       } 
      } 
     } catch { 
      throw TMDBErrors.ParsingError 
     } 
     return movies 
    } 

我认为必须改变这一行,或者必须删除。

的JSONObject [ “成果”]

我需要你们的帮助,谢谢!

回答

1

您的JSON没有任何results参数..所以你不需要它了..

 do { 
     if let jsonArray = try NSJSONSerialization.JSONObjectWithData(jsonData, options: .AllowFragments) as? [[String: AnyObject]] { 
      for i in jsonArray { 
       var properties = [String: AnyObject]() 
       properties[JSONKeys.id] = i[JSONKeys.id] 
       properties[JSONKeys.title] = i[JSONKeys.title] 
       properties[JSONKeys.posterPath] = i[JSONKeys.posterPath] 
       let movie = Movie(properties: properties) 
       movies.append(movie) 
      } 
     } 
    } catch { 
     throw TMDBErrors.ParsingError 
    } 
+1

@El V2.0队长谢谢你的工作,我会aprove你的答案。 – SwiftDeveloper