为什么JSON.parse("string")失败 [英] Why does JSON.parse("string") fail

查看:75
本文介绍了为什么JSON.parse("string")失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 JSON规范,字符串是合法的JSON值.

According to the JSON spec a string is a legitimate JSON value.

那为什么会发生这种情况?

So why does this happen?

推荐答案

您实际上是将裸词string传递给该函数,该函数当然不是有效的JSON.要实际传递 value "string,您需要注意JavaScript.

You are actually passing the bare word string in to the the function which of course is not valid JSON. To actually pass in the value "string" you need to be careful with your JavaScript.

尝试一下:

JSON.parse("\"string\"")

必须将多余的一对引号转义,以便它们成为您传递给函数的值的一部分.

The extra pair of quotes must be escaped so they become part of the value you pass in to the function.

这篇关于为什么JSON.parse("string")失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆