How to decode JSON with integer as key in Swift 4?
up vote
0
down vote
favorite
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
add a comment |
up vote
0
down vote
favorite
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
Possible duplicate of Swift 4 JSON Codable ids as keys
– Cristik
5 hours ago
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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
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
json swift swift4
asked 5 hours ago
Leonard
1601111
1601111
Possible duplicate of Swift 4 JSON Codable ids as keys
– Cristik
5 hours ago
add a comment |
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
add a comment |
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]
add a comment |
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.
add a comment |
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]
add a comment |
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]
add a comment |
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]
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]
answered 5 hours ago
Sweeper
59.6k967132
59.6k967132
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited 4 hours ago
Ahmad F
15.2k93975
15.2k93975
answered 5 hours ago
Tobi
1,097315
1,097315
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Possible duplicate of Swift 4 JSON Codable ids as keys
– Cristik
5 hours ago