PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, July 25, 2022

[FIXED] How to decode some strange JSON data in Swift?

 July 25, 2022     decode, json, swift     No comments   

Issue

My JSON file is like this:

> {
       "skill_list": [
        {
            "Skill": 78
        },
        {
            "Skill": 57
        },
        "None"
    ]
},
{
       "skill_list": [
        {
            "Skill": 12
        },
        "None",
        "None"
    ]
},

I don't know how to define a struct to this JSON data, and I don't want to use SwiftyJSON package.

Is there a way to define a struct to decode this JSON data?

Just like

JSONDecoder().decode(MyStruct.self, from: data)

the whole JSON is like:

{
"armor": {
  "param": [
    {
      "skill_list": [
         {
          "Skill": 56
        },
        {
          "Skill": 4
        },
        "None",
        "None",
        "None"
      ]
    },
    {
      "skill_list": [
        {
          "Skill": 103
        },
        "None",
        "None",
        "None",
        "None"
      ]
    }
    ]
}
}

Solution

finally i got this, the code is :

 let myJson = """
        {
        "armor": {
          "param": [
            {
              "skill_list": [
                {
                  "Skill": 56
                },
                "None",
                "None",
                "None",
                "None"
              ]
            },
            {
              "skill_list": [
                {
                  "Skill": 103
                },
                "None",
                "None",
                "None",
                "None"
              ]
            }
            ]
        }
        }
        """

        struct Armor: Decodable {
            var armor: ArmorClass
        }

        struct ArmorClass: Decodable {
            var param: [ArmorInfo]
        }
        struct ArmorInfo: Decodable {
            var skill_list: [MetadataType]
        }

        enum MetadataType: Codable {
          case Skill(SkillType)
          case string(String)

          init(from decoder: Decoder) throws {
            let container = try decoder.singleValueContainer()
            do {
                self = try .Skill(container.decode(SkillType.self))
            } catch DecodingError.typeMismatch {
              do {
                self = try .string(container.decode(String.self))
              } catch DecodingError.typeMismatch {
                throw DecodingError.typeMismatch(MetadataType.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Encoded payload not of an expected type"))
              }
            }
          }

        }
        struct SkillType: Codable {
            var Skill: Int
        }
        
        let jsonData = myJson.data(using: .utf8)!
        let decodeData = try! JSONDecoder().decode(Armor.self, from: jsonData)
        
        if case let MetadataType.Skill(value) = decodeData.armor.param[0].skill_list[0] {
            print(value.Skill)
        }
        if case let MetadataType.string(value) = decodeData.armor.param[0].skill_list[1] {
            print("String: \(value)")
        }


Answered By - Chr1s78
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing