2017-12-06 13 views
0

以下の機能でjsonデータをデコードしようとしています。私は最初に別のJson形式を持っていましたが、動作していましたが、私がjson形式を変更すると、動作しませんでした。構造コードでも変更を試みますが、何も機能しませんでした。私は何が欠けていますか?スウィフト4のJSONデータをデコードするとnilが返されます

{ 
"status": 200, 
"results": [ 
    { 
     "group_matched": false, 
     "distance_mi": 4, 
     "content_hash": "1rT2lirUapfrYIYxSR2u0GtmRSLPsVki9kFj4ugs8JIq6", 
     "common_friends": [], 
     "common_likes": [], 
     "common_friend_count": 0, 
     "common_like_count": 0, 
     "connection_count": 0, 
     "_id": "5a2805ba779f34267d32d8b0", 
     "bio": "", 
      "birth_date": "1997-12-09T16:28:29.761Z", 
      "name": "John", 
     }, 

第二JSON::

{ 
    "meta": { 
     "status": 200 
    }, 
    "data": { 
     "results": [ 
      { 
       "type": "user", 
       "user": { 
        "_id": "545001b33bf179416a30bf7f", 
        "bio": "Ä°nstagram:", 
        "birth_date": "1992-12-09T17:12:49.957Z", 
        "name": "", 
        "photos": [ 
         { 
          "id": "2eb1beec-6180-4d58-90fd-5f076da96af9", 
          "url": "", 
          "processedFiles": [ 
           { 
            "url": "", 
            "height": 640, 
            "width": 640 
           }, 
           { 
            "url": "", 
            "height": 320, 
            "width": 320 
           }, 
           { 
            "url": "", 
            "height": 172, 
            "width": 172 
           }, 
           { 
            "url": "", 
            "height": 84, 
            "width": 84 
           } 
          ] 
         }, 
         { 
          "id": "89d2bc07-d244-457b-b610-26ecd4e9e86d", 
          "url": "", 
          "processedFiles": [ 
           { 
            "url": "", 
            "height": 640, 
            "width": 640 
           }, 
           { 
            "url": "", 
            "height": 320, 
            "width": 320 
           }, 
           { 
            "url": "", 
            "height": 172, 
            "width": 172 
           }, 
           { 
            "url": "", 
            "height": 84, 
            "width": 84 
           } 
          ] 
         }, 
         { 
          "id": "0fcafc7e-d6e7-4cc5-891a-735971d6a5b2", 
          "url": "", 
          "processedFiles": [ 
           { 
            "url": "", 
            "height": 640, 
            "width": 640 
           }, 
           { 
            "url": "g", 
            "height": 320, 
            "width": 320 
           }, 
           { 
            "url": "", 
            "height": 172, 
            "width": 172 
           }, 
           { 
            "url": "", 
            "height": 84, 
            "width": 84 
           } 
          ] 
         }, 
         { 
          "id": "bac74531-09e7-4c5b-ac6c-cab36c4be587", 
          "url": "", 
          "processedFiles": [ 
           { 
            "url": "", 
            "height": 640, 
            "width": 640 
           }, 
           { 
            "url": "", 
            "height": 320, 
            "width": 320 
           }, 
           { 
            "url": "", 
            "height": 172, 
            "width": 172 
           }, 
           { 
            "url": "", 
            "height": 84, 
            "width": 84 
           } 
          ] 
         }, 
         { 
          "id": "3c216244-946a-4c58-8670-cc12c05801cb", 
          "url": "", 
          "processedFiles": [ 
           { 
            "url": "", 
            "height": 640, 
            "width": 640 
           }, 
           { 
            "url": "", 
            "height": 320, 
            "width": 320 
           }, 
           { 
            "url": "", 
            "height": 172, 
            "width": 172 
           }, 
           { 
            "url": "", 
            "height": 84, 
            "width": 84 
           } 
          ] 
         }, 
         { 
          "id": "-8672-488d-8b7e-f30b4560c56b", 
          "url": "", 
          "processedFiles": [ 
           { 
            "url": "", 
            "height": 640, 
            "width": 640 
           }, 
           { 
            "url": "", 
            "height": 320, 
            "width": 320 
           }, 
           { 
            "url": "", 
            "height": 172, 
            "width": 172 
           }, 
           { 
            "url": "", 
            "height": 84, 
            "width": 84 
           } 
          ] 
         } 
        ], 
        "gender": 1, 

構造体コード:

struct Photo: Codable{ 
    var url: String? 
    var processedFiles: [processedFiles]? 
} 
struct User: Codable{ 
    var group_matched: Bool? 
    var distance_mi: Int? 
    var common_friend_count: Int? 
    var name:String? 
    var profile_picture: String? 
    var instagram_id: String? 
    var photos: [Photo]? 
} 
class Results : Codable { 
    var results: [User] = [] 
    static let sharedResults = Results() 
    private init() { } 
    var type: String? 

    func populateData(sender: Results){ 
     print(sender) 
     results += sender.results 
    } 
} 

まずJSON(それはあまりにも第二のフォーマットで作業する必要がありますので、コードは最初にJSON形式のためです)

デコードコード:

let jsonData = JSON(data: response.data!) 
       let jsonData2 = try? JSONSerialization.data(withJSONObject: jsonData["data"].object) 
       print(JSON(data: jsonData2!)) 

       let decoder = JSONDecoder() 
       var response_class = Results.sharedResults 
       response_class = try decoder.decode(Results.self, from: jsonData2!) 
       Results.sharedResults.populateData(sender: response_class) 

EDIT:2番目のJSONを編集しました。これで配列の配列が作成されます。どのように2番目の配列のデコーダキーを実装できますか?

+0

のではなく、それらのスクリーンショットのようにJSONを投稿するにuser辞書を含めることnestedContainerでカスタム初期化子を記述しますテキスト。助けてくれる人は誰も、JSONテキストをテスト目的で再入力するつもりはありません。 – vadian

+0

申し訳ありませんが投稿しました。テキストとしてのJSON。 –

答えて

0

この場合、私は親オブジェクト

let jsonString = """ 
{ 
    "meta": { 
     "status": 200 
    }, 
    "data": { 
     "results": [ 
     { 
      "type": "user", 
      "user": { 
       "_id": "545001b33bf179416a30bf7f", 
       "bio": "Ä°nstagram:hello", 
       "birth_date": "1992-12-09T17:12:49.957Z", 
       "name": "Hello" 
      }, 

      "group_matched": false, 
      "distance_mi": 4 
     } 
     ] 
    } 
} 
""" 

struct Root : Decodable { 
    let meta : [String:Int] 
    let data : Result 
} 

struct Result : Decodable { 
    let results : [User] 
} 

struct User : Decodable { 

    let type : String 
    let groupMatched : Bool 
    let distanceMi : Int 

    let id, bio, birthDate, name : String 

    private enum CodingKeys : String, CodingKey { 
     case type, user, groupMatched = "group_matched", distanceMi = "distance_mi" 
    } 

    private enum UserKeys: String, CodingKey { 
     case id = "_id", bio, birthDate = "birth_date", name 
    } 

    init(from decoder: Decoder) throws { 
     let container = try decoder.container(keyedBy: CodingKeys.self) 
     type = try container.decode(String.self, forKey: .type) 
     groupMatched = try container.decode(Bool.self, forKey: .groupMatched) 
     distanceMi = try container.decode(Int.self, forKey: .distanceMi) 

     let userInfo = try container.nestedContainer(keyedBy: UserKeys.self, forKey: .user) 
     id = try userInfo.decode(String.self, forKey: .id) 
     bio = try userInfo.decode(String.self, forKey: .bio) 
     birthDate = try userInfo.decode(String.self, forKey: .birthDate) 
     name = try userInfo.decode(String.self, forKey: .name) 
    } 
} 

do { 
    let data = Data(jsonString.utf8) 
    let decoder = JSONDecoder() 
    let root = try decoder.decode(Root.self, from: data) 
    print(root)  
} catch { 
    print("error: ", error) 
} 
+0

ありがとう、それは働いています。データフィールドを渡すことはできますか? –

+0

*パス*とはどういう意味ですか? – vadian

+0

私の質問を少し更新しましたか? –

関連する問題