解析格式错误的 json 字符串,该字符串周围没有双引号(Java 脚本)
Parsing malformed json string which doesn't have double quotes around it (Java Script)
{ ValidationError: { device_uuid: [ [Object] ] } }
我喜欢将此字符串转换为 JSON 格式,就像
{ "ValidationError": { "device_uuid": [ [Object] ] } }
无论如何,我可以从格式错误的 JSON 字符串中获得此结果吗?
假设您确定格式不正确的字符串是安全的,并且只是格式不正确的 JSON(即不打算执行任何其他 javascript(,您可以先评估然后 JSON.stringify 它。
JSON.stringify(eval('(' + myString + ')'));
我找到了非常酷的JavaScript库。https://github.com/freethenation/durable-json-lint
它帮助我格式错误的 json 字符串到格式良好的 json 字符串!
durableJsonLint = require('durable-json-lint');
console.log(durableJsonLint('{name:"value", ''array'':[call(), 0x11]}'))
// The above code would print the following to the console
{
"json":'{"name":"value", "array":[null, 17]}',
"errors":[{
"column":1,
"description":"Keys must be double quoted in Json. Did you mean '"name'"?",
"lineNumber":1,
"status":"correctable"
},{
"column":15,
"description":"Json strings must use double quotes",
"lineNumber":1,
"status":"correctable"
},{
"column":24,
"description":"You can not make function calls in Json. Do you think I am a fool?",
"lineNumber":1,
"status":"fail"
},{
"column":32,
"description":"Invalid Json number",
"lineNumber":1,
"status":"correctable"
}
]
}
相关文章:
- 如何将字符串值从php页面发送到java脚本页面
- 内联javascript与"<脚本>"字符串错误地关闭了脚本标记
- 为什么脚本标记被分解为两个字符串
- 替换字符串的脚本;在某些网站上不起作用
- listbox不会将字符串传递给google脚本
- 如何在 java 脚本中将字符串日期转换为 GMT
- jQuery AJAX调用PHP脚本并返回字符串
- 传递php字符串作为php脚本中调用的javascript函数的参数
- 如何使用参数调用java脚本函数,其中参数来自字符串变量
- 如何向客户端返回带有双引号的字符串?脚本将其解析为JSON对象
- 使用java脚本提取字符串
- 一旦设置了PHP查询字符串,如何运行jQuery脚本
- 编写脚本以在提交之前将复选框值连接到字符串中
- 如何在脚本中拆分字符串
- 谷歌脚本如果文件名包含x字符串日志“;x被发现“;缺少括号
- Google Spreadsheets脚本可以删除找到字符串的任何行
- Jquery:在Jquery的(字符串)变量中执行Jquery脚本
- JS/Jquery:字符串到单词的文本分割脚本使用字典和最长匹配
- 解析格式错误的 json 字符串,该字符串周围没有双引号(Java 脚本)
- 如何让杰克逊在输出字符串中转义脚本>