1
{
  "0": {
    "name": "legaldoc.pdf",
    "cmisid": "yib5C-w92PPtxTBlXl4UJ8oDBthDtAU9mKN5kh2_KrQ"
  },
  "1": {
    "name": "persdoc.pdf",
    "cmisid": "dqAnrdNMXGTz1RbOMI37OY6tH9xMdxiTnz6wEl2m-VE"
  },
  "2": {
    "name": "certdoc.pdf",
    "cmisid": "6d7DuhldQlnb0JSjXlZb9mMOjxV3E_ID-ynJ0QRPMOA"
  }
}

How do I use Swift 4 Codable to parse JSON data like that? the problem is that the keys are Int array. How do I set CodingKeys for this?

4
  • 1
    Why do you say the keys are an Int array? There's no array of any kind in the JSON you posted. Commented Jan 6, 2018 at 5:10
  • 1
    Please look at the keys [0, 1, 2, 3], those JSON data was returned from database. Because this database doesn't support arrays in JSON like Firebase database, it will automatically add a Int key for each element like a Int array. Commented Jan 6, 2018 at 5:19
  • Again, there is no array in the JSON you posted. The JSON in your question is a dictionary that contains keys and values and those values are each another dictionary. No array. Commented Jan 6, 2018 at 5:20
  • How do I parse this by codable when the keys are numeric and sequential? Commented Jan 6, 2018 at 5:33

1 Answer 1

1

As mentioned in the comments there is no array. All collection types are dictionaries.

You can decode it as Swift dictionary. To get an array map the result to the values of the sorted keys

let jsonString = """
{
    "0": {
        "name": "legaldoc.pdf",
        "cmisid": "yib5C-w92PPtxTBlXl4UJ8oDBthDtAU9mKN5kh2_KrQ"
    },
    "1": {
        "name": "persdoc.pdf",
        "cmisid": "dqAnrdNMXGTz1RbOMI37OY6tH9xMdxiTnz6wEl2m-VE"
    },
    "2": {
        "name": "certdoc.pdf",
        "cmisid": "6d7DuhldQlnb0JSjXlZb9mMOjxV3E_ID-ynJ0QRPMOA"
    }
}
"""

struct Item : Codable {
    let name, cmisid : String
}

do {
    let data = Data(jsonString.utf8)
    let result = try JSONDecoder().decode([String: Item].self, from: data)
    let keys = result.keys.sorted()
    let array = keys.map{ result[$0]! }
    print(array)

} catch {
    print(error)
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your solution and letting me know that the swift dictionary also conform CODABLE protocol. The reason why I mentioned the Int array in the comments is that the data type is in array the database. When database return JSON, the keys(int and sequent) have to be added. So Array becomes to dictionary in JSON file. Is there a way to add some methods in Codable Init method which can transfer dictionary to array during Codable decoding? Or set an Int array as CodingKeys for "0" "1" "2"
Of course you can write a custom initializer but it's impossible to use CodingKeys to decode arbitrary dictionary keys . What is the expected result?
The expected result is same as your example, just move the methods to custom initializer that convert Dictionary to Array. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.