为什么我不尝试在JSON工作中排除引号? [英] Why doesn't my attempt to escape quotation marks in JSON work?

查看:109
本文介绍了为什么我不尝试在JSON工作中排除引号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JQuery.parseJSON函数解析JSON字符串,因为我在代码中做了很多次。在这种特殊情况下,我得到:未捕获语法错误:意外的令牌R 。在JSON格式的String中,唯一存在的大写字母R就在转义的引号后面,就是这样的。 ... \R ... 似乎太多的巧合是由这个以外的其他东西造成的,但据我所知,我完全遵循json.org所述的正确语法。



编辑:



我尝试手动删除 \在硬编码测试中,字符串格式完美地转换成一个正确的Javascript对象。换句话说,我的 \绝对是这里的问题...

  var myObject = $ .parseJSON(myString); 

编辑2: strong>



我的String的问题区域在这里显示,无论是工作还是不工作状态,首先是有问题的:

  {lineID:33,boxID:10,title:我的文本与\Ruining Part \ b  

然后工作:

 code> {lineID:33,boxID:10,title:我的文本与破坏部分} 

最后我如何将我的javabean对象格式化为JSON字符串。

  String jsonObjectAsString = new Gson ().toJson(myJavaBeanObject); 


解决方案

你可能需要要转义字符串中的反斜杠,如果它是硬编码的,那么得到解析的最后一个字符串有一个反斜杠,后面跟着一个双引号,否则浏览器会认为你试图逃避你的字符串中的一个双引号,它什么都不做。



所以将你的字符串改为:

  ... \\R ... 


I am parsing a JSON-string with the JQuery.parseJSON function, as I have done lot's of times in my code. On this particular case, though, I get: Uncaught SyntaxError: Unexpected token R. The only upper case R that exists, in my JSON-formatted String, comes right after an escaped quotation mark, ... \"R ... like this. It seems like too much of a coincidence to be caused by anything other than this, but as far as I can tell, I have completely followed the proper syntax as described on json.org.

EDIT:

I've tried to manually remove the occurrances of \" in a hardcoded test, and the string formats perfectly into a proper Javascript object. In other words, my \" is definitely the problem here...

var myObject = $.parseJSON(myString);

EDIT 2:

the problematic area of my String is here displayed, both in working, and not working condition. first the problematic one:

{"lineID":33,"boxID":10,"title":"My text with the \"Ruining Part\""}

Then the working one:

{"lineID":33,"boxID":10,"title":"My text with the Ruining Part"}

Finally how i format my javabean object into JSON string.

String jsonObjectAsString = new Gson().toJson(myJavaBeanObject);

解决方案

You probably need to escape the backslash in your string, if it is hardcoded, so that the final string that gets parsed has a single backslash followed by a double quote. Otherwise, the browser thinks you are trying to escape a double quote in your string, which does nothing.

So change your string to:

...\\"R...

这篇关于为什么我不尝试在JSON工作中排除引号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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