JSON,替换引号和斜杠,但是通过什么? [英] JSON, replace quotes and slashes, but by what?

查看:146
本文介绍了JSON,替换引号和斜杠,但是通过什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个忘恩负义的任务,就是在Java中手动构建一个JSON String,没有任何框架,只需一个StringBuilder。我知道这很糟糕,但它只是原型的一部分,下次我会做得更好。

I have the ungrateful task to build a JSON String in Java, manually, without any framework, just a StringBuilder. I know this is bad, but it is only part of a prototype, I will do this better next time.

我的问题:我怎样才能把字符串 - > 有些文字引用< - 进入JSON对象?

My question: How can I put the String -> "Some text WITH quotes" <- into a JSON object?

当然, {key:由于未转义的引号,某些带引号的文字} 无效json。

我想我有在这里使用String.replace,但是我可以用什么替换引号?斜杠/的问题相同。什么是正确的替代?

I think I have to use String.replace here, but by what can I replace the quotes? Same question for the slash "/". What is the proper replacement?

谢谢

推荐答案

"\"Some text WITH quotes\", slashes: /foo and backslashes \\foo"

转换为:

"Some text WITH quotes", slashes: /foo and backslashes \foo

您可以使用 StringEscapeUtils.escapeJavaScript( ) > apache-commons Lang 2.6或 StringEscapeUtils.escapeEcmaScript() ,为你做出逃避的艰苦工作。

You can use StringEscapeUtils.escapeJavaScript() in apache-commons Lang 2.6 or StringEscapeUtils.escapeEcmaScript() in 3.4 to do the hard-work of escaping for you.

这篇关于JSON,替换引号和斜杠,但是通过什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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