代码:
let decodedJsonDict:JSON = JSON(data: response.data) print("decodedJsonDict=\(decodedJsonDict)") |
调试结果:
decodedJsonDict={ "data" : { "phone" : "13800000002", "updated" : "2015-12-04T07:19:24Z", "active" : true, "invitations" : [ "contact-1a1d9607-c7af-4cc2-b7dc-5ea6519d20bb", "contact-45f8eee4-a21b-42a4-b6de-bf3dad33dc52", "contact-6a3d6937-d7de-4adc-9634-f1da07666034", "contact-6fdcf6dd-6aa8-4dd6-8515-ce043734eb6f", "contact-b1991997-1c81-4f50-b75c-0a7cf3a4a9b6", "contact-bb9d70ec-82b7-459c-b13a-bd2f4dc70d8c", "contact-ca155341-9d3e-4b10-9b18-0c5269ef36e5", "contact-d945b43e-0c0a-4945-8162-e0ffc0ee32b2", "contact-e2dd7d8c-93ea-428b-928a-0e210f37c9be" ], "owned" : [ "team-2d93ca07-dc86-4a42-9e58-a17861784659" ], "_id" : "user-1f4857d8-5ea8-434e-8e7c-3f8d20f1f2cf", "uploading" : [ ], ], ], ], }, "message" : "ok", "code" : 200 } |
中,要把data部分的值,再次变成JSON。
swift conver to NSData
swift JSON to NSData
期间发现:
dictionaryObject
dictionary
dictionaryValue
都不行:
decodedJsonDict={ "data" : { "phone" : "13800000002", "updated" : "2015-12-04T07:19:24Z", "active" : true, "invitations" : [ "contact-1a1d9607-c7af-4cc2-b7dc-5ea6519d20bb", "contact-45f8eee4-a21b-42a4-b6de-bf3dad33dc52", "contact-6a3d6937-d7de-4adc-9634-f1da07666034", "contact-6fdcf6dd-6aa8-4dd6-8515-ce043734eb6f", "contact-b1991997-1c81-4f50-b75c-0a7cf3a4a9b6", "contact-bb9d70ec-82b7-459c-b13a-bd2f4dc70d8c", "contact-ca155341-9d3e-4b10-9b18-0c5269ef36e5", "contact-d945b43e-0c0a-4945-8162-e0ffc0ee32b2", "contact-e2dd7d8c-93ea-428b-928a-0e210f37c9be" ], "owned" : [ "team-2d93ca07-dc86-4a42-9e58-a17861784659" ], "_id" : "user-1f4857d8-5ea8-434e-8e7c-3f8d20f1f2cf", "uploading" : [ ], "visiting" : [ ], "pinned" : [ ], "created" : "2015-12-04T07:19:02Z", "contacts" : [ "contact-0abb4267-b6e2-4e37-8fa9-3ec7d35bffab", "contact-0eafe800-ad4b-4dc9-ac09-886acd875385", "contact-41f34c7e-3b54-44aa-803b-8fbda38b8ff3", "contact-49c9f03c-7d37-41c5-b813-96a44c3a0f5c", "contact-65fe762f-2e53-44c1-8f7d-fe03fce7d0d0", "contact-6733727a-b259-43b0-a878-1c9a1b8031db", "contact-8563411b-7d07-4ca2-9de7-58f7a0d3fac0", "contact-acf4946f-6a59-456a-a685-f36f8852e590", "contact-f1620cc0-144b-4df1-be49-e8fae2894157" ], "joined" : [ "team-1318502b-3e1c-4465-a2a7-b90a1f6252b0", "team-167bf4c8-269e-42ad-b1a9-48a383e26169", "team-19722466-1595-413b-ab14-f54c409cda84", "team-2d93ca07-dc86-4a42-9e58-a17861784659", "team-3e510b30-d2c6-4eea-b28f-7708cfb514f4", "team-5140abe4-fdb6-49e8-b8be-42a0808191b1", "team-99a181f8-a4eb-4465-b94a-61f989ba9075", "team-c2da699f-5d8e-455b-a3f7-52b8ff1d27f2", "team-fcb285c1-3ab6-4f61-b4a2-212253a1afe7", "team-ff074dd6-30d8-4597-99c4-664b6a848df8" ], "received" : [ ], "email" : "", "name" : "尉妹" }, "message" : "ok", "code" : 200 } decodedJsonDict["data"]={ "visiting" : [ ], "active" : true, "owned" : [ "team-2d93ca07-dc86-4a42-9e58-a17861784659" ], "pinned" : [ ], "contacts" : [ "contact-0abb4267-b6e2-4e37-8fa9-3ec7d35bffab", "contact-0eafe800-ad4b-4dc9-ac09-886acd875385", "contact-41f34c7e-3b54-44aa-803b-8fbda38b8ff3", "contact-49c9f03c-7d37-41c5-b813-96a44c3a0f5c", "contact-65fe762f-2e53-44c1-8f7d-fe03fce7d0d0", "contact-6733727a-b259-43b0-a878-1c9a1b8031db", "contact-8563411b-7d07-4ca2-9de7-58f7a0d3fac0", "contact-acf4946f-6a59-456a-a685-f36f8852e590", "contact-f1620cc0-144b-4df1-be49-e8fae2894157" ], "email" : "", "invitations" : [ "contact-1a1d9607-c7af-4cc2-b7dc-5ea6519d20bb", "contact-45f8eee4-a21b-42a4-b6de-bf3dad33dc52", "contact-6a3d6937-d7de-4adc-9634-f1da07666034", "contact-6fdcf6dd-6aa8-4dd6-8515-ce043734eb6f", "contact-b1991997-1c81-4f50-b75c-0a7cf3a4a9b6", "contact-bb9d70ec-82b7-459c-b13a-bd2f4dc70d8c", "contact-ca155341-9d3e-4b10-9b18-0c5269ef36e5", "contact-d945b43e-0c0a-4945-8162-e0ffc0ee32b2", "contact-e2dd7d8c-93ea-428b-928a-0e210f37c9be" ], "updated" : "2015-12-04T07:19:24Z", "name" : "尉妹", "joined" : [ "team-1318502b-3e1c-4465-a2a7-b90a1f6252b0", "team-167bf4c8-269e-42ad-b1a9-48a383e26169", "team-19722466-1595-413b-ab14-f54c409cda84", "team-2d93ca07-dc86-4a42-9e58-a17861784659", "team-3e510b30-d2c6-4eea-b28f-7708cfb514f4", "team-5140abe4-fdb6-49e8-b8be-42a0808191b1", "team-99a181f8-a4eb-4465-b94a-61f989ba9075", "team-c2da699f-5d8e-455b-a3f7-52b8ff1d27f2", "team-fcb285c1-3ab6-4f61-b4a2-212253a1afe7", "team-ff074dd6-30d8-4597-99c4-664b6a848df8" ], "_id" : "user-1f4857d8-5ea8-434e-8e7c-3f8d20f1f2cf", "uploading" : [ ], "phone" : "13800000002", "received" : [ ], "created" : "2015-12-04T07:19:02Z" } decodedJsonDict["data"].object={ "_id" = "user-1f4857d8-5ea8-434e-8e7c-3f8d20f1f2cf"; active = 1; contacts = ( "contact-0abb4267-b6e2-4e37-8fa9-3ec7d35bffab", "contact-0eafe800-ad4b-4dc9-ac09-886acd875385", "contact-41f34c7e-3b54-44aa-803b-8fbda38b8ff3", "contact-49c9f03c-7d37-41c5-b813-96a44c3a0f5c", "contact-65fe762f-2e53-44c1-8f7d-fe03fce7d0d0", "contact-6733727a-b259-43b0-a878-1c9a1b8031db", "contact-8563411b-7d07-4ca2-9de7-58f7a0d3fac0", "contact-acf4946f-6a59-456a-a685-f36f8852e590", "contact-f1620cc0-144b-4df1-be49-e8fae2894157" ); created = "2015-12-04T07:19:02Z"; email = ""; invitations = ( "contact-1a1d9607-c7af-4cc2-b7dc-5ea6519d20bb", "contact-45f8eee4-a21b-42a4-b6de-bf3dad33dc52", "contact-6a3d6937-d7de-4adc-9634-f1da07666034", "contact-6fdcf6dd-6aa8-4dd6-8515-ce043734eb6f", "contact-b1991997-1c81-4f50-b75c-0a7cf3a4a9b6", "contact-bb9d70ec-82b7-459c-b13a-bd2f4dc70d8c", "contact-ca155341-9d3e-4b10-9b18-0c5269ef36e5", "contact-d945b43e-0c0a-4945-8162-e0ffc0ee32b2", "contact-e2dd7d8c-93ea-428b-928a-0e210f37c9be" ); joined = ( "team-1318502b-3e1c-4465-a2a7-b90a1f6252b0", "team-167bf4c8-269e-42ad-b1a9-48a383e26169", "team-19722466-1595-413b-ab14-f54c409cda84", "team-2d93ca07-dc86-4a42-9e58-a17861784659", "team-3e510b30-d2c6-4eea-b28f-7708cfb514f4", "team-5140abe4-fdb6-49e8-b8be-42a0808191b1", "team-99a181f8-a4eb-4465-b94a-61f989ba9075", "team-c2da699f-5d8e-455b-a3f7-52b8ff1d27f2", "team-fcb285c1-3ab6-4f61-b4a2-212253a1afe7", "team-ff074dd6-30d8-4597-99c4-664b6a848df8" ); name = "\U5c09\U59b9"; owned = ( "team-2d93ca07-dc86-4a42-9e58-a17861784659" ); phone = 13800000002; pinned = ( ); received = ( ); updated = "2015-12-04T07:19:24Z"; uploading = ( ); visiting = ( ); } decodedJsonDict["data"].string=nil decodedJsonDict["data"].dictionary=Optional(["visiting": [ ], "active": true, "owned": [ "team-2d93ca07-dc86-4a42-9e58-a17861784659" ], "pinned": [ ], "contacts": [ "contact-0abb4267-b6e2-4e37-8fa9-3ec7d35bffab", "contact-0eafe800-ad4b-4dc9-ac09-886acd875385", "contact-41f34c7e-3b54-44aa-803b-8fbda38b8ff3", "contact-49c9f03c-7d37-41c5-b813-96a44c3a0f5c", "contact-65fe762f-2e53-44c1-8f7d-fe03fce7d0d0", "contact-6733727a-b259-43b0-a878-1c9a1b8031db", "contact-8563411b-7d07-4ca2-9de7-58f7a0d3fac0", "contact-acf4946f-6a59-456a-a685-f36f8852e590", "contact-f1620cc0-144b-4df1-be49-e8fae2894157" ], "email": , "invitations": [ "contact-1a1d9607-c7af-4cc2-b7dc-5ea6519d20bb", "contact-45f8eee4-a21b-42a4-b6de-bf3dad33dc52", "contact-6a3d6937-d7de-4adc-9634-f1da07666034", "contact-6fdcf6dd-6aa8-4dd6-8515-ce043734eb6f", "contact-b1991997-1c81-4f50-b75c-0a7cf3a4a9b6", "contact-bb9d70ec-82b7-459c-b13a-bd2f4dc70d8c", "contact-ca155341-9d3e-4b10-9b18-0c5269ef36e5", "contact-d945b43e-0c0a-4945-8162-e0ffc0ee32b2", "contact-e2dd7d8c-93ea-428b-928a-0e210f37c9be" ], "updated": 2015-12-04T07:19:24Z, "name": 尉妹, "joined": [ "team-1318502b-3e1c-4465-a2a7-b90a1f6252b0", "team-167bf4c8-269e-42ad-b1a9-48a383e26169", "team-19722466-1595-413b-ab14-f54c409cda84", "team-2d93ca07-dc86-4a42-9e58-a17861784659", "team-3e510b30-d2c6-4eea-b28f-7708cfb514f4", "team-5140abe4-fdb6-49e8-b8be-42a0808191b1", "team-99a181f8-a4eb-4465-b94a-61f989ba9075", "team-c2da699f-5d8e-455b-a3f7-52b8ff1d27f2", "team-fcb285c1-3ab6-4f61-b4a2-212253a1afe7", "team-ff074dd6-30d8-4597-99c4-664b6a848df8" ], "_id": user-1f4857d8-5ea8-434e-8e7c-3f8d20f1f2cf, "uploading": [ ], "phone": 13800000002, "received": [ ], "created": 2015-12-04T07:19:02Z]) decodedJsonDict["data"].dictionaryValue=["visiting": [ ], "active": true, "owned": [ "team-2d93ca07-dc86-4a42-9e58-a17861784659" ], "pinned": [ ], "contacts": [ "contact-0abb4267-b6e2-4e37-8fa9-3ec7d35bffab", "contact-0eafe800-ad4b-4dc9-ac09-886acd875385", "contact-41f34c7e-3b54-44aa-803b-8fbda38b8ff3", "contact-49c9f03c-7d37-41c5-b813-96a44c3a0f5c", "contact-65fe762f-2e53-44c1-8f7d-fe03fce7d0d0", "contact-6733727a-b259-43b0-a878-1c9a1b8031db", "contact-8563411b-7d07-4ca2-9de7-58f7a0d3fac0", "contact-acf4946f-6a59-456a-a685-f36f8852e590", "contact-f1620cc0-144b-4df1-be49-e8fae2894157" ], "email": , "invitations": [ "contact-1a1d9607-c7af-4cc2-b7dc-5ea6519d20bb", "contact-45f8eee4-a21b-42a4-b6de-bf3dad33dc52", "contact-6a3d6937-d7de-4adc-9634-f1da07666034", "contact-6fdcf6dd-6aa8-4dd6-8515-ce043734eb6f", "contact-b1991997-1c81-4f50-b75c-0a7cf3a4a9b6", "contact-bb9d70ec-82b7-459c-b13a-bd2f4dc70d8c", "contact-ca155341-9d3e-4b10-9b18-0c5269ef36e5", "contact-d945b43e-0c0a-4945-8162-e0ffc0ee32b2", "contact-e2dd7d8c-93ea-428b-928a-0e210f37c9be" ], "updated": 2015-12-04T07:19:24Z, "name": 尉妹, "joined": [ "team-1318502b-3e1c-4465-a2a7-b90a1f6252b0", "team-167bf4c8-269e-42ad-b1a9-48a383e26169", "team-19722466-1595-413b-ab14-f54c409cda84", "team-2d93ca07-dc86-4a42-9e58-a17861784659", "team-3e510b30-d2c6-4eea-b28f-7708cfb514f4", "team-5140abe4-fdb6-49e8-b8be-42a0808191b1", "team-99a181f8-a4eb-4465-b94a-61f989ba9075", "team-c2da699f-5d8e-455b-a3f7-52b8ff1d27f2", "team-fcb285c1-3ab6-4f61-b4a2-212253a1afe7", "team-ff074dd6-30d8-4597-99c4-664b6a848df8" ], "_id": user-1f4857d8-5ea8-434e-8e7c-3f8d20f1f2cf, "uploading": [ ], "phone": 13800000002, "received": [ ], "created": 2015-12-04T07:19:02Z] decodedJsonDict["data"].dictionaryObject=Optional(["visiting": ( ), "active": 1, "owned": ( "team-2d93ca07-dc86-4a42-9e58-a17861784659" ), "pinned": ( ), "contacts": ( "contact-0abb4267-b6e2-4e37-8fa9-3ec7d35bffab", "contact-0eafe800-ad4b-4dc9-ac09-886acd875385", "contact-41f34c7e-3b54-44aa-803b-8fbda38b8ff3", "contact-49c9f03c-7d37-41c5-b813-96a44c3a0f5c", "contact-65fe762f-2e53-44c1-8f7d-fe03fce7d0d0", "contact-6733727a-b259-43b0-a878-1c9a1b8031db", "contact-8563411b-7d07-4ca2-9de7-58f7a0d3fac0", "contact-acf4946f-6a59-456a-a685-f36f8852e590", "contact-f1620cc0-144b-4df1-be49-e8fae2894157" ), "email": , "invitations": ( "contact-1a1d9607-c7af-4cc2-b7dc-5ea6519d20bb", "contact-45f8eee4-a21b-42a4-b6de-bf3dad33dc52", "contact-6a3d6937-d7de-4adc-9634-f1da07666034", "contact-6fdcf6dd-6aa8-4dd6-8515-ce043734eb6f", "contact-b1991997-1c81-4f50-b75c-0a7cf3a4a9b6", "contact-bb9d70ec-82b7-459c-b13a-bd2f4dc70d8c", "contact-ca155341-9d3e-4b10-9b18-0c5269ef36e5", "contact-d945b43e-0c0a-4945-8162-e0ffc0ee32b2", "contact-e2dd7d8c-93ea-428b-928a-0e210f37c9be" ), "updated": 2015-12-04T07:19:24Z, "name": 尉妹, "joined": ( "team-1318502b-3e1c-4465-a2a7-b90a1f6252b0", "team-167bf4c8-269e-42ad-b1a9-48a383e26169", "team-19722466-1595-413b-ab14-f54c409cda84", "team-2d93ca07-dc86-4a42-9e58-a17861784659", "team-3e510b30-d2c6-4eea-b28f-7708cfb514f4", "team-5140abe4-fdb6-49e8-b8be-42a0808191b1", "team-99a181f8-a4eb-4465-b94a-61f989ba9075", "team-c2da699f-5d8e-455b-a3f7-52b8ff1d27f2", "team-fcb285c1-3ab6-4f61-b4a2-212253a1afe7", "team-ff074dd6-30d8-4597-99c4-664b6a848df8" ), "_id": user-1f4857d8-5ea8-434e-8e7c-3f8d20f1f2cf, "uploading": ( ), "phone": 13800000002, "received": ( ), "created": 2015-12-04T07:19:02Z]) |
最后,发现JSON库中,有个object属性,是可以直接拿到JSON键值的整个对象的,所以就可以了:
【总结】
代码:
let decodedJsonDict:JSON = JSON(data: response.data) print("decodedJsonDict=\(decodedJsonDict)") print("decodedJsonDict[\"data\"]=\(decodedJsonDict["data"])") print("decodedJsonDict[\"data\"].object=\(decodedJsonDict["data"].object)") // print("decodedJsonDict[\"data\"].object.data=\(decodedJsonDict["data"].object.data)") //nil //both string or dictonary all ok let decodedDataJsonDict:JSON = JSON(decodedJsonDict["data"].object) print("decodedDataJsonDict=\(decodedDataJsonDict)") |
输出:
decodedJsonDict={ "data" : { "phone" : "13800000002", "updated" : "2015-12-04T07:19:24Z", "active" : true, "invitations" : [ "contact-1a1d9607-c7af-4cc2-b7dc-5ea6519d20bb", "contact-45f8eee4-a21b-42a4-b6de-bf3dad33dc52", "contact-6a3d6937-d7de-4adc-9634-f1da07666034", "contact-6fdcf6dd-6aa8-4dd6-8515-ce043734eb6f", "contact-b1991997-1c81-4f50-b75c-0a7cf3a4a9b6", "contact-bb9d70ec-82b7-459c-b13a-bd2f4dc70d8c", "contact-ca155341-9d3e-4b10-9b18-0c5269ef36e5", "contact-d945b43e-0c0a-4945-8162-e0ffc0ee32b2", "contact-e2dd7d8c-93ea-428b-928a-0e210f37c9be" ], "owned" : [ "team-2d93ca07-dc86-4a42-9e58-a17861784659" ], "_id" : "user-1f4857d8-5ea8-434e-8e7c-3f8d20f1f2cf", "uploading" : [ ], ], ], ], }, "message" : "ok", "code" : 200 } decodedJsonDict["data"]={ "visiting" : [ ], ], "joined" : [ "team-1318502b-3e1c-4465-a2a7-b90a1f6252b0", "team-167bf4c8-269e-42ad-b1a9-48a383e26169", "team-19722466-1595-413b-ab14-f54c409cda84", "team-2d93ca07-dc86-4a42-9e58-a17861784659", "team-3e510b30-d2c6-4eea-b28f-7708cfb514f4", "team-5140abe4-fdb6-49e8-b8be-42a0808191b1", "team-99a181f8-a4eb-4465-b94a-61f989ba9075", "team-c2da699f-5d8e-455b-a3f7-52b8ff1d27f2", "team-fcb285c1-3ab6-4f61-b4a2-212253a1afe7", "team-ff074dd6-30d8-4597-99c4-664b6a848df8" ], "_id" : "user-1f4857d8-5ea8-434e-8e7c-3f8d20f1f2cf", "uploading" : [ ], ], ], ], "joined" : [ "team-1318502b-3e1c-4465-a2a7-b90a1f6252b0", "team-167bf4c8-269e-42ad-b1a9-48a383e26169", "team-19722466-1595-413b-ab14-f54c409cda84", "team-2d93ca07-dc86-4a42-9e58-a17861784659", "team-3e510b30-d2c6-4eea-b28f-7708cfb514f4", "team-5140abe4-fdb6-49e8-b8be-42a0808191b1", "team-99a181f8-a4eb-4465-b94a-61f989ba9075", "team-c2da699f-5d8e-455b-a3f7-52b8ff1d27f2", "team-fcb285c1-3ab6-4f61-b4a2-212253a1afe7", "team-ff074dd6-30d8-4597-99c4-664b6a848df8" ], "_id" : "user-1f4857d8-5ea8-434e-8e7c-3f8d20f1f2cf", "uploading" : [ ], ], } |
转载请注明:在路上 » 【已解决】Swift中把JSON的data域值变成新的JSON变量