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
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
asnestedUnkeyedContainer
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.
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.
You have to deserialise the Json like this
json = try JSONSerialization.jsonObject(with: data!) as? [Any]
and after that you need to show the program how to set the data up like this.
guard let item = json! as? [String: Any],
var rank = item["..."] as! whatever kind of variable you whant else {
return
}
and then depending on how many jsonobjects you have you should loop through them and when you have done this you should send the item to a func that puts the values in your own class if you whant them in a object. ps when you are showing the program how to setup the Json object you can go through the item object as a dictionary as i am showing you in the second codebit.
来源:https://stackoverflow.com/questions/48749302/decoding-json-array-of-different-types-in-swift