Gson - 自动引用(")转义? [英] Gson - Automatic quote (") escaping?

查看:269
本文介绍了Gson - 自动引用(")转义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的Java EE服务器上使用GSON为视图提供一些json。
在一些对象中,我有很长的文本,可以包含任何内容(比如'What a'great'news!')。



默认情况下,GSON不会转义双引号,所以它不会生成有效的JSON。



有没有这样做的好方法?

解决方案

也许我不理解你的问题,但是我能够让GSON在没有任何设置或更改的情况下用引号处理字符串。

  import com.google.gson.Gson; 

public class GSONTest {

public String value;

public static void main(String [] args){
Gson g = new Gson();
GSONTest gt = new GSONTest();
gt.value =这是引用字符串的\test \;
System.out.println(String:+ gt.value);
System.out.println(JSON:+ g.toJson(gt));


$ / code $ / pre
$ b $输出:

 字符串:这是引用字符串的测试
JSON:{value:这是引用的\test \字符串}

也许我不明白你在问什么?


I'm using GSON on my Java EE server to provide some json to views. In some object, I have long text, that can contains anything (like 'What a "great" news!').

I'm supprised that by default GSON doesn't escape the double quote, so it doesn't generate a valid JSON.

Is there a good way of doing this ?

解决方案

Maybe I'm not understanding your question, but I was able to get GSON to handle Strings with quotes without any settings or changes.

import com.google.gson.Gson;

public class GSONTest {

    public String value;

    public static void main(String[] args) {
        Gson g = new Gson();
        GSONTest gt = new GSONTest();
        gt.value = "This is a \"test\" of quoted strings";
        System.out.println("String: " + gt.value);
        System.out.println("JSON: " + g.toJson(gt));
    }
}

Output:

String: This is a "test" of quoted strings
JSON: {"value":"This is a \"test\" of quoted strings"}

Maybe I don't understand what you're asking?

这篇关于Gson - 自动引用(")转义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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