编辑代码

let s = "```json\n{\n  \"name\":\"李如飞\",\n  \"sex\":\"男\",\n  \"age\":\"70\",\n  \"hospital\":\"江苏省南通大学附属医院\",\n  \"report_dt\":\"2007-2-5 00:00:00\",\n  \"dept\":\"泌外\",\n  \"report_type\":\"3\"\n}\n```"

function test() {
    // s = s.replace(/\\n/g, '')
    // console.log(s)
    const match = /```(json)?(.*)```/s.exec(s);
    if (!match) {
        return JSON.parse(s);
    } else {
        return JSON.parse(match[2]);
    }
}

// console.log(test())

const b =  "{\n  \"name\": \"xx\",\n  \"sex\": \"xx\",\n  \"age\": \"xx\",\n  \"hospital\": \"新红医中心医院\",\n  \"report_dt\": \"null\",\n  \"dept\": \"急诊科\",\n  \"report_type\": 2\n}"
const result = JSON.parse(b)
console.log(result)
console.log(result.hospital)