English 中文(简体)
如何在没有太多的引述的情况下将JsonObject编成序列?
原标题:How to serialize a JsonObject without too much quotes?

I m 在 com.google.gson上写了一幅小块液晶。 JsonObject.

当我编篡我的Json时,我收到:

{"key1":"value1","key2":"value2","key3":"{"innerKey":"value3"}"}

我如何摆脱多余的引言?

我的法典:

public class JsonBuilder {
    private final JsonObject json = new JsonObject();
    private final Map<String, JsonBuilder> children = newHashMap();

    public String toJson() {
        for (Map.Entry<String, JsonBuilder> entry : children.entrySet()) {
            String value = entry.getValue().toJson();
            add(entry.getKey(), value);
        }
        children.clear();

        return json.toString();
    }

    public JsonBuilder add(String key, String value) {
        json.addProperty(key, value);
        return this;
    }

    public JsonBuilder add(String key, JsonBuilder value) {
        Preconditions.checkArgument(!children.containsKey(key));
        children.put(key, value);
        return this;
    }
}

String json = new JsonBuilder()
    .add("key1", "value1")
    .add("key2", "value2")
    .add("key3", new JsonBuilder()
        .add("innerKey", "value3"))
    .toJson();
问题回答

这里的问题是,你在类别中编造混杂的强硬和物体。 而是尽可能贴近<条码>JsonObject,并且只是稍微扩展。 如果您希望填上<代码>JsonBuilder,则如上,对<代码>的改动最小。 JsonObject:

package com.craigmj.gson;

import java.util.HashMap;

import com.google.gson.JsonObject;

class JsonBuilder {
    public final JsonObject json = new JsonObject();

    public String toJson() {
        return json.toString();
    }

    public JsonBuilder add(String key, String value) {
        json.addProperty(key, value);
        return this;
    }

    public JsonBuilder add(String key, JsonBuilder value) {
        json.add(key, value.json);
        return this;
    }
}

public class GsonTest {

    public static void main(String[] args) {

        System.out.println(new JsonBuilder()
            .add("key1", "value1")
            .add("key2", "value2")
            .add("key3", new JsonBuilder()
            .add("innerKey", "value3"))
            .toJson());
    }

}

请注意,I ve使json财产公诸于世,这样,如果你想要具备全部森功能,你就能够继续使用所有森松方法。 (当然,你或许会有一个跳板) 不幸的是,我们可以提一下<代码>JsonObject。 这是最后的。

使用gson.toJsonTree()

You may take a look at this https://github.com/FasterXML/jackson-jr#writing-with-composers

Jackson jr是完全由Jackson Databind组成的契约性替代物。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签