简体   繁体   中英

Swift how to express JSON in structs

I want to express the following JSON and convert to swift structs
1) I get error in the third line full_plan "comma is missing". I don't know why a comma is required? I need help fixing it 2) If that is fixed will the structs shown below is accurate to convert to JSON?

Please note: add_ons may be missing in the JSON for some plans, so second plan shown does not have add_ons.

Basically I am asking help to fix the JSON and the struct for swift.

{ 
    "id": "100",

    "plans":
    [
        "full_plan":
        {       
            "plan":
            [
                { "plan_type": "Legacy" },
                { "contract_duration_months": "12" }
            ],
            "add_ons" :
            [
                { "parking": "yes"},
                { "washerDryer": "no" }
            ]
        },
        "full_plan":
        {
            "plan":
            [
                { "plan_type": "New" },
                { "contract_duration_months": "0" }
            ]
        }
    ]
}

 struct TopPlan : Decodable {
        var uniqueId: String?
        var Plans: [FullPlan]?
        enum CodingKeys : String, CodingKey {
            case uniqueId = "id"
            case Plans = "plans"
        }
    }
    struct FullPlan: Decodable {
        var Plan: PlanJSON?
        var freePlan: AddOnsJSON?
        enum CodingKeys : String, CodingKey {
            case pricedPlan = "plan"
            case freePlan = "add_ons"
        }
    }
    struct PlanJSON: Decodable {
        var planType: String?
        var duration: String?
        enum CodingKeys : String, CodingKey {
            case planType = "plan_type"
            case duration = "contract_duration_months"
        }
    }
    struct AddOnsJSON: Decodable {
        var parking: String?
        var washerDryer: String?
        enum CodingKeys : String, CodingKey {
            case parking = "parking"
            case washerDryer = "washerDryer"
        }
    }

Short answer: your current JSON is invalid syntax.

You are using "full_plan" as a key (which would be fine if "plans" was an object) inside an array. Arrays in JavaScript (and thus in JSON) are unkeyed. You should either remove "full_plan" and just use the object that it refers to like "plans": [{}, {}, etc] , or if you need to keep the object key wrap the entire item in curly braces such as "plans": [{ "full_plan": {}}, { "full_plan": {}}, etc]

You should build this up from the bottom (and if you post your question in a form that is executable in a Playground you will get answers faster). As it stands your plan JSON is unfortunate. Your struct states that you want a hash and you provide it with an array of hashes (which should be merged to get what you want). Try it like this:

import Cocoa

let jsonData = """
{ "plan_type": "Legacy",
  "contract_duration_months": "12"
}
""".data(using: .utf8)!

struct PlanJSON: Decodable {
    var planType: String?
    var duration: String?
    enum CodingKeys : String, CodingKey {
        case planType = "plan_type"
        case duration = "contract_duration_months"
    }
}

do {
    let plan = try JSONDecoder().decode(PlanJSON.self, from: jsonData)
    print(plan)
} catch {
    print(error)
}

That way you will be given enough information to further fix your JSON, but the rest looks ok.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM