【问题标题】:GSON library ignoring + symbol in the value fieldGSON 库忽略值字段中的 + 符号
【发布时间】:2017-01-09 15:58:26
【问题描述】:

我有一个简单的 json,我使用 GSON 库将其转换为 Java。

{
    "name" : "DOB",
    "operator" : "<",
    "value" : "CURRENTDATE + 3"
}

当我执行 gson.fromJson 时,值部分返回为“CURRENTDATE 3”。 + 符号被忽略。

gson 调用是否需要一些设置?

【问题讨论】:

  • 我不确定该符号是否被忽略,或者是否已转换为其他内容。无论如何,我提供的链接看起来是一个很好的起点。
  • 我检查了 符号,它们被正确考虑。奇怪的是,它忽略了 + 符号。我也尝试设置 disableHtmlEscaping() ...但是,它没有帮助。

标签: java json gson


【解决方案1】:

gson 的 Maven 依赖项

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.0</version>
</dependency>

代码片段:

public static void main(String[] args) {
    String jsonString = "{\"name\" : \"DOB\", \"operator\" : \"<\", \"value\" : \"CURRENTDATE + 3\"}";
    Gson gson = new GsonBuilder().disableHtmlEscaping().create();
    Test test = gson.fromJson(jsonString, Test.class);
    System.out.println(test);
}

public class Test {
    String name;
    String operator;
    String value;

    @Override
    public String toString() {
        return "Test [name=" + name + ", operator=" + operator + ", value=" + value + "]";
    }
}

输出:

Test [name=DOB, operator=<, value=CURRENTDATE + 3]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-23
    • 1970-01-01
    • 2023-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多