Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gson转换JSON的坑 #11

Open
johnnian opened this issue Jul 3, 2017 · 0 comments
Open

Gson转换JSON的坑 #11

johnnian opened this issue Jul 3, 2017 · 0 comments
Labels

Comments

@johnnian
Copy link
Owner

johnnian commented Jul 3, 2017

在做微信小程序,需要生成子页面的二维码,并且在二维码中需要附带一些参数,例如:page/test/index?q=123

代码如下:

String path = "page/test/index?q=123";
String json = new Gson().toJson(path);

Gson 在转换成JSON的时候, = 号会变成 \u003d,导致生成的二维码不对,无法顺利跳转到具体页面。

出现这个的原因是: Gson会把html标签,转换为Unicode转义字符。

两种解决方法:

方法一:使用 JSONObject

String json = new JSONObject().fromObject(path).toString();

方法二:使用 GsonBuilder

String json = new GsonBuilder().disableHtmlEscaping().create().toJson(path);
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant