javascript JSON.parse解析双引号嵌套单引号格式的数据

JSON.parse() 方法用于将一个 JSON 字符串转换为对象。

>str1 = "{'distance': 1, 'update_time': 'None', 'n_homalt_N_het': '13/3822', 'ratio': '-', 'quality': '451'}"
"{'distance': 1, 'update_time': 'None', 'n_homalt_N_het': '13/3822', 'ratio': '-', 'quality': '451'}"
>JSON.parse(str1)
VM1366:1 Uncaught SyntaxError: Unexpected token ' in JSON at position 1
    at JSON.parse (<anonymous>)
    at <anonymous>:1:6
(anonymous) @ VM1365:1

解决方法1

生成标准格式的json数据。

str1 = '{"distance": 1, "update_time": "None", "n_homalt_N_het": "13/3822", "ratio": "-", "quality": "451"}'

解决方法2

>str2 = str1.replace(/'/g, '"')
"{"distance": 1, "update_time": "None", "n_homalt_N_het": "13/3822", "ratio": "-", "quality": "451"}"
>JSON.parse(str2)
{distance: 1, update_time: "None", n_homalt_N_het: "13/3822", ratio: "-", quality: "451"}
>

解决方法2

string.split()

The JSON Data Interchange Standard says:
A string is a sequence of zero or more Unicode characters, wrapped indouble quotes

你可能感兴趣的:(JavaScript,javascript,json)