Javascript 语法错误:JSON 中位置 1 的意外标记 o
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38380462/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SyntaxError: Unexpected token o in JSON at position 1
提问by Soniya Mohan
I parse few data using a type class in my controller I'm getting data as follows:
我使用控制器中的类型类解析了一些数据我得到的数据如下:
{
"data":{
"userList":[
{
"id":1,
"name":"soni"
}
]
},
"status":200,
"config":{
"method":"POST",
"transformRequest":[
null
],
"transformResponse":[
null
],
"url":"/home/main/module/userlist",
"headers":{
"rt":"ajax",
"Tenant":"Id:null",
"Access-Handler":"Authorization:null",
"Accept":"application/json, text/plain, */*"
}
},
"statusText":"OK"
}
I tried to store the data like this
我试图像这样存储数据
var userData = _data;
var newData = JSON.parse(userData).data.userList;
How can I extract the user list to a new variable?
如何将用户列表提取到新变量?
回答by Timo
The JSON you posted looks fine, however in your code, it is most likely not a JSON string anymore, but already a JavaScript object. This means, no more parsing is necessary.
您发布的 JSON 看起来不错,但是在您的代码中,它很可能不再是 JSON 字符串,而是已经是 JavaScript 对象。这意味着不再需要解析。
You can test this yourself, e.g. in Chrome's console:
您可以自己测试,例如在 Chrome 的控制台中:
new Object().toString()
// "[object Object]"
JSON.parse(new Object())
// Uncaught SyntaxError: Unexpected token o in JSON at position 1
JSON.parse("[object Object]")
// Uncaught SyntaxError: Unexpected token o in JSON at position 1
JSON.parse()
converts the input into a string. The toString()
method of JavaScript objects by default returns [object Object]
, resulting in the observed behavior.
JSON.parse()
将输入转换为字符串。toString()
JavaScript 对象的方法默认返回[object Object]
,导致观察到的行为。
Try the following instead:
请尝试以下操作:
var newData = userData.data.userList;
回答by huruji
the first parameters of function JSON.parse
should be a String, and your data is a JavaScript object, so it will convert to a String [object object]
, you should use JSON.stringify
before pass the data
函数的第一个参数JSON.parse
应该是一个字符串,你的数据是一个 JavaScript 对象,所以它会转换为一个 String [object object]
,你应该JSON.stringify
在传递数据之前使用
JSON.parse(JSON.stringify(userData))
回答by Sukhchain
Just above JSON.parse
, use:
就在上面JSON.parse
,使用:
var newData = JSON.stringify(userData)
回答by Kousha
Don't ever use JSON.parse
without wrapping it in try-catch
block:
不要JSON.parse
在不将其包装在try-catch
块中的情况下使用:
// payload
let userData = null;
try {
userDate = JSON.parse(payload);
} catch (e) {
// You can read e for more info
// Let's assume the error is that we already have parsed the payload
// So just return that
userData = payload;
}
// Now userData is the parsed result
回答by М.Б.
Well, I meant that I need to parse object like this: var jsonObj = {"first name" : "fname"}
. But, I don't actually. Because it's already an JSON.
嗯,我的意思是我需要解析的对象是这样的:var jsonObj = {"first name" : "fname"}
。但是,我实际上没有。因为它已经是一个 JSON。
回答by Yergalem
Unexpected 'O' error is thrown when JSON data or String happens to get parsed.
当 JSON 数据或字符串碰巧被解析时,会抛出意外的“O”错误。
If it's string, it's already stringfied. Parsing ends up with Unexpected 'O' error.
如果它是字符串,则它已经被字符串化了。解析以意外的“O”错误告终。
I faced similar( although in different context), I solved the following error by removing JSON Producer.
我遇到了类似的情况(尽管在不同的上下文中),我通过删除 JSON Producer 解决了以下错误。
@POST
@Produces({ **MediaType.APPLICATION_JSON**})
public Response login(@QueryParam("agentID") String agentID , Officer aOffcr ) {
return Response.status(200).entity("OK").build();
}
The response contains "OK"string return. The annotation marked as @Produces({ **MediaType.APPLICATION_JSON})** tries to parse the string to JSON format which results in Unexpected 'O'.
响应包含“OK”字符串返回。标记为@Produces({ **MediaType.APPLICATION_JSON})**的注释尝试将字符串解析为 JSON 格式,这会导致Unexpected 'O'。
Removing @Produces({ MediaType.APPLICATION_JSON})works fine. Output : OK
删除@Produces({ MediaType.APPLICATION_JSON})工作正常。输出:好的
Beware: Also, on client side, if you make ajax request and use JSON.parse("OK"), it throws Unexpected token 'O'
注意:另外,在客户端,如果您发出 ajax 请求并使用 JSON.parse("OK"),它会抛出 Unexpected token 'O'
Ois the first letter of the string
O是字符串的第一个字母
JSON.parse(object) compares with jQuery.parseJSON(object);
JSON.parse(object) 与 jQuery.parseJSON(object) 比较;
JSON.parse('{ "name":"Yergalem", "city":"Dover"}'); --- Works Fine
JSON.parse('{ "name":"Yergalem", "city":"Dover"}'); - - 工作正常