我试图解析这个JSON字符串:
{"查询":"我今天的日程安排","输入":"时间线","标题" :"今天 事件:""时间":[" 2015-07-06 \ N20:30:00&#34],"总结":[&#34 ;每周会议+ 展示&告诉(环聊)"],"说明":["每周子弹(20分钟): "]}
这是一个有效的JSON(在jsonformatter.curiousconcept.com上查看)。但是,我收到了erorr:
SyntaxError:意外的令牌
in(file angular.js):
function fromJson(json) {
return isString(json)
? JSON.parse(json)
: json;
}
有人有想法吗?
答案 0 :(得分:4)
问题是文本中的\n
,您需要将其转义为\\n
var json = '{"query": "my schedule today","type": "timeline","title": "Today events:","time":["2015-07-06\\n20:30:00"],"summary":["Weekly meeting + Show & Tell (Hangout)"],"description":["Weekly Bullets (20 minutes): "]}'
console.log(JSON.parse(json))
snippet.log(JSON.stringify(JSON.parse(json)))
<!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
如果您正在使用的字符串是外部呼叫的结果,并且您无法手动将\n
更改为\\n
,则可以通过简单的替换来实现:
json = json.replace(/\\n/g, "\\\n");
答案 1 :(得分:0)
在这里,你的字符串中有一个\n
标记,我们将其删除:
var data = '{"query": "my schedule today","type": "timeline","title": "Today events:","time":["2015-07-06\n20:30:00"],"summary":["Weekly meeting + Show & Tell (Hangout)"],"description":["Weekly Bullets (20 minutes): "]}'.replace('\n', '');
var data = JSON.parse(data);
alert(data.query);
希望这有帮助。