TheBearF8
TheBearF8

Reputation: 395

Decoding JSON array of different types in Swift

I'm trying to decode the following JSON Object

{
    "result":[
             {
             "rank":12,
             "user":{ 
                     "name":"bob","age":12 
                    } 
             }, 
             {
             "1":[ 
                  {
                    "name":"bob","age":12
                  },
                  {
                   "name":"tim","age":13
                  }, 
                  {
                   "name":"tony","age":12
                  }, 
                  {
                   "name":"greg","age":13
                  } 
                 ] 
            } 
           ] 
}

struct userObject { var name: String var age: Int }

Basically a JSON Array with two different object types

{ "rank":12, "user": {userObject} }

and a "1" : array of [userObjects]

struct data: Decodable {
   rank: Int
   user: user
   1:    [user]  <-- this is one area Im stuck
}

Thanks in advance

Upvotes: 5

Views: 6476

Answers (2)

vadian
vadian

Reputation: 285069

Just for fun:

First you need structs for the users and the representation of the first and second dictionary in the result array. The key "1" is mapped to one

struct User : Decodable {
    let name : String
    let age : Int
}

struct FirstDictionary : Decodable {
    let rank : Int
    let user : User
}

struct SecondDictionary : Decodable {
    let one : [User]

    private enum CodingKeys: String, CodingKey { case one = "1" }
}

Now comes the tricky part:

  • First get the root container.
  • Get the container for result as nestedUnkeyedContainer because the object is an array.
  • Decode the first dictionary and copy the values.
  • Decode the second dictionary and copy the values.

    struct UserData: Decodable {
    
        let rank : Int
        let user : User
        let oneUsers : [User]
    
       private enum CodingKeys: String, CodingKey { case result }
    
        init(from decoder: Decoder) throws {
            let container = try decoder.container(keyedBy: CodingKeys.self)
            var arrayContainer = try container.nestedUnkeyedContainer(forKey: .result)
            let firstDictionary = try arrayContainer.decode(FirstDictionary.self)
            rank = firstDictionary.rank
            user = firstDictionary.user
            let secondDictionary = try arrayContainer.decode(SecondDictionary.self)
            oneUsers = secondDictionary.one
        }
    }
    

If this code is preferable over traditional manual JSONSerialization is another question.

Upvotes: 9

Patru
Patru

Reputation: 4551

If your JSON format is given then you are pretty much out of luck, since you will most likely have to parse your array as [Any] which is, to put it mildly, not very useful. If on the other hand you are able to modify the format of the JSON you should start from the other direction. Define your desired Swift object and encode it using JSONEncoder.encode(...) in order to quickly determine how your JSON should look like in order to make it parse in as typed a way as possible.

This approach will easily half your JSON handling code as your web service protocol will end up being structured much better. This will likely improve the structure of the overall system since it will yield a much more stable communication protocol.

Sadly enough this approach is not always possible which is when things get messy. Given your example you will be able to parse your code as

let st = """
{
    "result":[
        {
            "rank":12,
            "user":{
                "name":"bob",
                "age":12
            }
        },
        {
            "1":[
                {
                    "name":"bob","age":12
                },
                {
                    "name":"tim","age":13
                },
                {
                    "name":"tony","age":12
                },
                {
                    "name":"greg","age":13
                }
            ]
        }
    ]
}
"""

let jsonData1 = st.data(using: .utf8)!
let arbitrary = try JSONSerialization.jsonObject(with: jsonData1, options: .mutableContainers)

This will let you access your data with a bunch of casts as in

let dict = arbitrary as! NSDictionary
print(dict["result"])

you get the idea. not very useful as you would very much like to use the Codable protocol as in

 struct ArrayRes : Codable {
     let result : [[String:Any]]
 }

 let decoder1 = JSONDecoder()
 do {
     let addrRes = try decoder.decode(ArrayRes.self, from: jsonData1)
        print(addrRes)
     } catch {
        print("error on decode: \(error.localizedDescription)")
 }

Unfortunately this does not work since Any is not Codable for slightly obvious reasons.

I hope you are able to change your JSON protocol since the current one will be the root cause of lot of messy code.

Upvotes: 0

Related Questions