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();