0

I have following JSON:

[
{
 "id": 1,
 "type": "Feature",
 "geometry": {
     "type": "Point",
     "coordinates": [
         37.5741167,
         55.7636592
     ]
 },
 "properties": {
     "hintContent": "переулок Волков, 13с1",
     "balloonContentHeader": "переулок Волков, 13с1"
 }
]

I am trying to use JSONDecoder:

struct Point : Codable {
    let id: Int
    let type: String
    let properties: Properties
}
struct Properties : Codable {
    let hintContent: String
    let balloonContentHeader: String
}
struct Points : Codable {
    var data : [Point]
}



func parse(fileName: String) {

    let url = Bundle.main.url(forResource: fileName, withExtension: "json")
    let data = try? Data(contentsOf: url!)
    if let jsonPoints = try? JSONDecoder().decode([Point].self, from: data!) {
        print(jsonPoints)
    }
}

What is wrong?

2
  • Post your real JSON string. This one is missing a closing curly bracket. Should be }} ] Commented Jul 18, 2021 at 20:37
  • 5
    "What is wrong?" It's the other way around. You tell us what happens. Commented Jul 18, 2021 at 20:40

1 Answer 1

5
  1. Make sure your JSON is valid -- this one has a missing }.

  2. Paste your (valid) JSON into app.quicktype.io to generate models ie. make sure that your models match the JSON. In the event that your JSON isn't valid, that site will warn you about it as well.

  3. Always use do/try/catch and not try? so that you can get meaningful errors when JSON decoding fails.


let data = """
[
{
 "id": 1,
 "type": "Feature",
 "geometry": {
     "type": "Point",
     "coordinates": [
         37.5741167,
         55.7636592
     ]
 },
 "properties": {
     "hintContent": "t",
     "balloonContentHeader": "t"
 }
}
]
""".data(using: .utf8)

struct Point: Codable {
    let id: Int
    let type: String
    let geometry: Geometry
    let properties: Properties
}

// MARK: - Geometry
struct Geometry: Codable {
    let type: String
    let coordinates: [Double]
}

// MARK: - Properties
struct Properties: Codable {
    let hintContent, balloonContentHeader: String
}

func parse(fileName: String) {
    do {
        let jsonPoints = try JSONDecoder().decode([Point].self, from: data!)
        print(jsonPoints)
    } catch {
        print(error)
    }
    
}

Without the adjusted JSON, your original code would generate this error in the catch block:

The given data was not valid JSON....Badly formed object around character 224.

Sign up to request clarification or add additional context in comments.

4 Comments

What's let data = """ ?
@ElTomato """ starts a multi-line String literal in Swift
@Dmitry did this answer your question? If so, you can mark it is correct using the green checkmark.
Thanks so much. The issue was other - JSON owner gave in id field intermittently String instead Int. I don't have ideas how to fix this with Codable struct. I had to read each field and check time. But at any case thanks so much. I pushed me to right direction.

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.