解析无效的JSON swift 5

问题描述 投票:0回答:1

我从外部api提取了无效的json:

{
    status: 'ok',
    pills: [{
            id: 1,
            name: "Мезим форте",
            img: "https://cloud.fdoctor.ru/test_task/static/mezim.jpg",
            desription: "Перед завтраком",
            dose: "По таблетке"
        },
        {
            id: 2,
            name: "Bioderma",
            img: "https://cloud.fdoctor.ru/test_task/static/bioderma.jpg",
            desription: "Во время еды"
            dose: "По 3 глотка"
        },
        {
            id: 3,
            name: "Гексорал, Аэрозоль"
            img: "https://cloud.fdoctor.ru/test_task/static/gecsoral.jpg",
            desription: "При острых болях"
            dose: "По 3 пшика"
        },
        {
            id: 4,
            name: "Тантум Верде, спрей"
            img: "https://cloud.fdoctor.ru/test_task/static/tantum.jpg",
            desription: "Каждые 4 часа в течении 7 дней"
            dose: "По таблетке"
        }
    ]
}

有没有办法用可编码协议解析它?

无法通过API修复。

ios json swift api codable
1个回答
0
投票

您可以使用以下reg ex替换项来清洗字符串并使它符合json规范

//Surround keys with quotes
let firstStep = input.replacingOccurrences(of: #"\s(\w+):{1}"#, 
                                           with: #""$1":"#, 
                                           options: .regularExpression)
//Fix status value
let json = firstStep.replacingOccurrences(of: "'", with: "\"")
© www.soinside.com 2019 - 2024. All rights reserved.