How to decode JSON with integer as key in Swift 4?











up vote
0
down vote

favorite












enter image description here



As you can see, the right hand data has keys like "7", "8" etc...
Now I know that these are not quite "integers" because they are numbers in String.
However, when I decode them, I need to use that key name as the variable name.
But, we cannot have number as a variable name.



What should I do in this case? How can I decode this?



Is it just better to re-create the dataset so that it does not have the number as the key? But then, what do people do when they do not have control the dataset?



The entire code is too long and irrelevant so I just took a screenshot.
If you want, you can see the JSON data here
https://api.myjson.com/bins/11r19i










share|improve this question






















  • Possible duplicate of Swift 4 JSON Codable ids as keys
    – Cristik
    5 hours ago















up vote
0
down vote

favorite












enter image description here



As you can see, the right hand data has keys like "7", "8" etc...
Now I know that these are not quite "integers" because they are numbers in String.
However, when I decode them, I need to use that key name as the variable name.
But, we cannot have number as a variable name.



What should I do in this case? How can I decode this?



Is it just better to re-create the dataset so that it does not have the number as the key? But then, what do people do when they do not have control the dataset?



The entire code is too long and irrelevant so I just took a screenshot.
If you want, you can see the JSON data here
https://api.myjson.com/bins/11r19i










share|improve this question






















  • Possible duplicate of Swift 4 JSON Codable ids as keys
    – Cristik
    5 hours ago













up vote
0
down vote

favorite









up vote
0
down vote

favorite











enter image description here



As you can see, the right hand data has keys like "7", "8" etc...
Now I know that these are not quite "integers" because they are numbers in String.
However, when I decode them, I need to use that key name as the variable name.
But, we cannot have number as a variable name.



What should I do in this case? How can I decode this?



Is it just better to re-create the dataset so that it does not have the number as the key? But then, what do people do when they do not have control the dataset?



The entire code is too long and irrelevant so I just took a screenshot.
If you want, you can see the JSON data here
https://api.myjson.com/bins/11r19i










share|improve this question













enter image description here



As you can see, the right hand data has keys like "7", "8" etc...
Now I know that these are not quite "integers" because they are numbers in String.
However, when I decode them, I need to use that key name as the variable name.
But, we cannot have number as a variable name.



What should I do in this case? How can I decode this?



Is it just better to re-create the dataset so that it does not have the number as the key? But then, what do people do when they do not have control the dataset?



The entire code is too long and irrelevant so I just took a screenshot.
If you want, you can see the JSON data here
https://api.myjson.com/bins/11r19i







json swift swift4






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 5 hours ago









Leonard

1601111




1601111












  • Possible duplicate of Swift 4 JSON Codable ids as keys
    – Cristik
    5 hours ago


















  • Possible duplicate of Swift 4 JSON Codable ids as keys
    – Cristik
    5 hours ago
















Possible duplicate of Swift 4 JSON Codable ids as keys
– Cristik
5 hours ago




Possible duplicate of Swift 4 JSON Codable ids as keys
– Cristik
5 hours ago












2 Answers
2






active

oldest

votes

















up vote
3
down vote













You can define your own coding keys:



struct A: Decodable {
let one: [Int]
let two: [Int]

enum CodingKeys : String, CodingKey {
// this basically means that the key "1" will correspond to the property "one"
case one = "1"
// key "2" will correspond to the property "two"
case two = "2"
}
}


Example:



let json = "{"1": [1,2,3], "2": [4,5,6]}"
let data = json.data(using: .utf8)
let decoder = JSONDecoder()
let a = try! decoder.decode(A.self, from: data!)
print(a.two) // [4,5,6]





share|improve this answer




























    up vote
    3
    down vote













    You can use CodingKey to achieve that.



    struct bus: Codable {
    var one: String?
    var two: String?
    var three: String?
    enum CodingKeys: String, CodingKey {
    case one = "1"
    case two = "2"
    case three = "3"
    }
    }


    it will decode those keys inside each case, you should be able to recognize now.






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














       

      draft saved


      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53202948%2fhow-to-decode-json-with-integer-as-key-in-swift-4%23new-answer', 'question_page');
      }
      );

      Post as a guest
































      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      3
      down vote













      You can define your own coding keys:



      struct A: Decodable {
      let one: [Int]
      let two: [Int]

      enum CodingKeys : String, CodingKey {
      // this basically means that the key "1" will correspond to the property "one"
      case one = "1"
      // key "2" will correspond to the property "two"
      case two = "2"
      }
      }


      Example:



      let json = "{"1": [1,2,3], "2": [4,5,6]}"
      let data = json.data(using: .utf8)
      let decoder = JSONDecoder()
      let a = try! decoder.decode(A.self, from: data!)
      print(a.two) // [4,5,6]





      share|improve this answer

























        up vote
        3
        down vote













        You can define your own coding keys:



        struct A: Decodable {
        let one: [Int]
        let two: [Int]

        enum CodingKeys : String, CodingKey {
        // this basically means that the key "1" will correspond to the property "one"
        case one = "1"
        // key "2" will correspond to the property "two"
        case two = "2"
        }
        }


        Example:



        let json = "{"1": [1,2,3], "2": [4,5,6]}"
        let data = json.data(using: .utf8)
        let decoder = JSONDecoder()
        let a = try! decoder.decode(A.self, from: data!)
        print(a.two) // [4,5,6]





        share|improve this answer























          up vote
          3
          down vote










          up vote
          3
          down vote









          You can define your own coding keys:



          struct A: Decodable {
          let one: [Int]
          let two: [Int]

          enum CodingKeys : String, CodingKey {
          // this basically means that the key "1" will correspond to the property "one"
          case one = "1"
          // key "2" will correspond to the property "two"
          case two = "2"
          }
          }


          Example:



          let json = "{"1": [1,2,3], "2": [4,5,6]}"
          let data = json.data(using: .utf8)
          let decoder = JSONDecoder()
          let a = try! decoder.decode(A.self, from: data!)
          print(a.two) // [4,5,6]





          share|improve this answer












          You can define your own coding keys:



          struct A: Decodable {
          let one: [Int]
          let two: [Int]

          enum CodingKeys : String, CodingKey {
          // this basically means that the key "1" will correspond to the property "one"
          case one = "1"
          // key "2" will correspond to the property "two"
          case two = "2"
          }
          }


          Example:



          let json = "{"1": [1,2,3], "2": [4,5,6]}"
          let data = json.data(using: .utf8)
          let decoder = JSONDecoder()
          let a = try! decoder.decode(A.self, from: data!)
          print(a.two) // [4,5,6]






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 5 hours ago









          Sweeper

          59.6k967132




          59.6k967132
























              up vote
              3
              down vote













              You can use CodingKey to achieve that.



              struct bus: Codable {
              var one: String?
              var two: String?
              var three: String?
              enum CodingKeys: String, CodingKey {
              case one = "1"
              case two = "2"
              case three = "3"
              }
              }


              it will decode those keys inside each case, you should be able to recognize now.






              share|improve this answer



























                up vote
                3
                down vote













                You can use CodingKey to achieve that.



                struct bus: Codable {
                var one: String?
                var two: String?
                var three: String?
                enum CodingKeys: String, CodingKey {
                case one = "1"
                case two = "2"
                case three = "3"
                }
                }


                it will decode those keys inside each case, you should be able to recognize now.






                share|improve this answer

























                  up vote
                  3
                  down vote










                  up vote
                  3
                  down vote









                  You can use CodingKey to achieve that.



                  struct bus: Codable {
                  var one: String?
                  var two: String?
                  var three: String?
                  enum CodingKeys: String, CodingKey {
                  case one = "1"
                  case two = "2"
                  case three = "3"
                  }
                  }


                  it will decode those keys inside each case, you should be able to recognize now.






                  share|improve this answer














                  You can use CodingKey to achieve that.



                  struct bus: Codable {
                  var one: String?
                  var two: String?
                  var three: String?
                  enum CodingKeys: String, CodingKey {
                  case one = "1"
                  case two = "2"
                  case three = "3"
                  }
                  }


                  it will decode those keys inside each case, you should be able to recognize now.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 4 hours ago









                  Ahmad F

                  15.2k93975




                  15.2k93975










                  answered 5 hours ago









                  Tobi

                  1,097315




                  1,097315






























                       

                      draft saved


                      draft discarded



















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53202948%2fhow-to-decode-json-with-integer-as-key-in-swift-4%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest




















































































                      Popular posts from this blog

                      Schultheiß

                      Liste der Kulturdenkmale in Wilsdruff

                      Android Play Services Check