[英]Add a new Json property to the Json String response
我有一个 Json 响应String
,如下所示。
{
"arr": [
{
"Value": "v1",
"id": 10456102,
"arr1": [
{
"date": "2017-09-02",
"c1": {
"a": 1287900,
"c": "USD"
}
},
{
"date": "2017-09-03",
"c1": {
"a": 1288000,
"c": "USD"
}
}
]
}
]
}
在这里,我尝试添加一个新属性flag=Set
,最终的 Json 如下所示:
{
"arr": [
{
"Value": "v1",
"id": 10456102,
"arr1": [
{
"date": "2017-09-02",
"c1": {
"a": 1287900,
"c": "USD"
}
},
{
"date": "2017-09-03",
"c1": {
"a": 1288000,
"c": "USD"
}
}
]
}
]
"flag":"set"
}
我试图获取 Json object 并添加了新属性。 但它没有附加这个新字段。
JsonElement jsonElement = new JsonParser().parse(jsonResponse);
jsonElement.getAsJsonObject().addProperty("flag", set);
jsonResponse
是一个String
。
您是否尝试过简单地添加如下属性:
JsonElement jsonElement = new JsonParser().parse(jsonResponse);
jsonElement.set = set;
或使用数组语法:
jsonElement["set"] = set;
此致。
您可以使用我的 object 库:
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
/**
*
* @author longdo
*/
public class JsonObject {
private static final JsonParser GSON_PARSER = new JsonParser();
private final com.google.gson.JsonObject gsonObject;
public JsonObject() {
this(new com.google.gson.JsonObject());
}
public JsonObject(com.google.gson.JsonObject gsonObject) {
this.gsonObject = gsonObject;
}
public JsonObject addProperty(String property, String value) {
this.gsonObject.addProperty(property, value);
return this;
}
public JsonObject addProperty(String property, Number value) {
this.gsonObject.addProperty(property, value);
return this;
}
public JsonObject addProperty(String property, Boolean value) {
this.gsonObject.addProperty(property, value);
return this;
}
public JsonObject add(String property, JsonObject value) {
this.gsonObject.add(property, value.toGson());
return this;
}
public JsonObject add(String property, JsonArray value) {
this.gsonObject.add(property, value.toGson());
return this;
}
public String getString(String property) {
JsonElement je = this.gsonObject.get(property);
if (je == null || je.isJsonNull()) {
return null;
}
return je.getAsString();
}
public int getInt(String property) throws NoSuchFieldException {
JsonElement je = this.gsonObject.get(property);
if (je == null) {
throw new NoSuchFieldException(property + " (getInt) not found.");
}
return je.getAsInt();
}
public int getInt(String property, int defaultValue) {
JsonElement je = this.gsonObject.get(property);
if (je == null) {
return defaultValue;
}
return je.getAsInt();
}
public long getLong(String property) throws NoSuchFieldException {
JsonElement je = this.gsonObject.get(property);
if (je == null) {
throw new NoSuchFieldException(property + " (getLong) not found.");
}
return je.getAsLong();
}
public long getLong(String property, long defaultValue) {
JsonElement je = this.gsonObject.get(property);
if (je == null) {
return defaultValue;
}
return je.getAsLong();
}
public boolean getBoolean(String property) throws NoSuchFieldException {
JsonElement je = this.gsonObject.get(property);
if (je == null) {
throw new NoSuchFieldException(property + " (getBoolean) not found.");
}
return je.getAsBoolean();
}
public JsonObject getObject(String property) {
JsonElement je = this.gsonObject.get(property);
if (je == null) {
return null;
}
return new JsonObject(je.getAsJsonObject());
}
public JsonArray getArray(String property) {
JsonElement je = this.gsonObject.get(property);
if (je == null) {
return null;
}
return new JsonArray(je.getAsJsonArray());
}
public com.google.gson.JsonObject toGson() {
return gsonObject;
}
public int size() {
return gsonObject.size();
}
@Override
public String toString() {
return gsonObject.toString();
}
public static JsonObject parse(String jsonString) {
if (jsonString == null || jsonString.isEmpty()) {
return null;
}
return new JsonObject(GSON_PARSER.parse(jsonString).getAsJsonObject());
}
}
和主要的 class:
String str = "your string";
JsonObject obj = JsonObject.parse(str);
obj.addProperty("flag", "set");
System.out.println(obj.toString());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.