Decoding JSON array of different types in Swift

后端 未结 3 863
夕颜
夕颜 2020-12-18 12:36

I\'m trying to decode the following JSON Object

{
    \"result\":[
             {
             \"rank\":12,
             \"user\":{ 
                     \"n         


        
3条回答
  •  离开以前
    2020-12-18 13:30

    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.

提交回复
热议问题