博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
解析JSON时出现“意外令牌o”错误[重复]
阅读量:2379 次
发布时间:2019-05-10

本文共 2153 字,大约阅读时间需要 7 分钟。

本文翻译自:

This question already has an answer here: 这个问题已经在这里有了答案:

  • 8 answers 8个答案

I am having a problem parsing simple JSON strings. 我在解析简单的JSON字符串时遇到问题。 I have checked them on and it shows that they are valid. 我已经在上检查了它们,并显示它们是有效的。 But when I try to parse them using either JSON.parse or the jQuery alternative it gives me the error unexpected token o : 但是,当我尝试使用JSON.parse或jQuery替代方法解析它们时,它给了我错误的unexpected token o

            

Note: I'm encoding my strings using json_encode() in PHP. 注意:我在PHP中使用json_encode()对字符串进行编码。


#1楼

参考:


#2楼

cur_ques_details已经是一个JS对象,您无需解析它


#3楼

Your data is already an object. 您的数据已经是一个对象。 No need to parse it. 无需解析。 The javascript interpreter has already parsed it for you. javascript解释器已经为您解析了它。

var cur_ques_details ={"ques_id":15,"ques_title":"jlkjlkjlkjljl"};document.write(cur_ques_details['ques_title']);

#4楼

The source of your error, however, is that you need to place the full JSON string in quotes. 但是,错误的根源是您需要将完整的JSON字符串放在引号中。 The following will fix your sample: 以下将修复您的示例:

                        

As the other respondents have mentioned, the object is already parsed into a JS object so you don't need to parse it. 正如其他受访者所提到的,该对象已经被解析为JS对象,因此您无需解析它。 To demonstrate how to accomplish the same thing without parsing, you can do the following: 为了演示如何在不解析的情况下完成同一件事,您可以执行以下操作:

                

#5楼

Response is already parsed, you don't need to parse it again. 响应已经解析,您无需再次解析。 if you parse it again it will give you " unexpected token o ". 如果您再次解析它,它将为您提供“ unexpected token o ”。 if you need to get it as string, you could use JSON.stringify() 如果需要以字符串形式获取它,则可以使用JSON.stringify()


#6楼

I had the same problem when I submitted data using jQuery AJAX: 使用jQuery AJAX提交数据时,我遇到了同样的问题:

$.ajax({   url:...   success:function(data){      //server response's data is JSON      //I use jQuery's parseJSON method       $.parseJSON(data);//it's ERROR   }});

If the response is JSON, and you use this method, the data you get is a JavaScript object, but if you use dataType:"text" , data is a JSON string. 如果响应为JSON,并且您使用此方法,则获取的数据为JavaScript对象,但如果使用dataType:"text" ,则数据为JSON字符串。 Then the use of $.parseJSON is okay. 这样就可以使用$.parseJSON

转载地址:http://ifexb.baihongyu.com/

你可能感兴趣的文章
主成分分析实现的一个心得
查看>>
一次svn数据库的崩溃错误的解决
查看>>
有关3S产业前景的一些思考
查看>>
“locktype”enum type 类型重定义问题的解决
查看>>
看好刘永行
查看>>
历史的另一种养分
查看>>
常见遥感卫星基本参数大全
查看>>
桌面程序调用Web Service应用实例
查看>>
原来生命可以如此张扬
查看>>
只剩下葛氏幽默
查看>>
"网络适配器本地连接没有有效ip地址配置"错误的解决办法
查看>>
在VS2005中搭配VSS6.0
查看>>
一次修复IncrediBuild Coordinator服务的经历
查看>>
反思对待新人的方式
查看>>
经历的一次诈骗
查看>>
编译pano13的一些注意事项
查看>>
略谈如何在对话框创建视图类画图
查看>>
使用xerces库的一个注意事项
查看>>
使用C#版本的gdal库打开hdf文件
查看>>
我们看人的眼光
查看>>