[英]GSON creating an Array of Objects
I am trying to create a patchRequest of the format : 我正在尝试创建以下格式的patchRequest:
'[
{
"op": "replace",
"path": "/path1",
"value": "val1"
},
{
"op": "replace",
"path": "/path2",
"value": [
{
"name": "val2"
}
]
}
]'
I have tried a lot of stupid things, like creating 2 json objects and Trying to add them to patchRequest object. 我尝试了很多愚蠢的事情,例如创建2个json对象并将其添加到patchRequest对象。 OR trying to create 2 patchRequestObj, then add them to patchRequest List (that defeats purpose since i am using patchRequest as my input).
或尝试创建2个patchRequestObj,然后将它们添加到patchRequest列表中(由于我使用patchRequest作为输入,因此无法达到目的)。 patchReq objects to JsonObjects, add to JSONArray and convert back to patchRequest (this fails).
将patchReq对象添加到JsonObjects,添加到JSONArray并转换回patchRequest(此操作失败)。
Could not find any documentation that is helpful for my case. 找不到对我的情况有用的任何文档。 Can anyone suggest the same to me.
谁能向我建议相同。
Thanks. 谢谢。
In pseudocode: 用伪代码:
mainArray = new JsonArray();
firstObject = new JsonObject();
firstObject.add("op", "replace");
firstObject.add("path", "/path1");
firstObject.add("value", "val1");
mainArray.add(firstObject);
secondObject = new JsonObject();
secondObject.add("op", "replace");
secondObject.add("path", "/path2");
innerArray = new JsonArray();
innerObject = new JsonObject();
innerObject.add("name", "val2");
innerArray.add(innerObject);
secondObject.add("value", innerArray);
mainArray.add(secondObject);
jsonString = mainArray.toJsonString();
As I guess, logic is; 我猜想,逻辑是;
value
json tag should be a primitive string. value
json标签应为原始字符串。 value
json tag must be an array of objects that has name
fields. value
json标签必须是具有name
字段的对象的数组。 For this case you can write a custom TypeAdapter : 对于这种情况,您可以编写一个自定义的TypeAdapter :
public class ValueTypeAdapter extends TypeAdapter<Value> {
@Override
public Value read(JsonReader in) throws IOException {
Value value = null;
JsonParser jsonParser = new JsonParser();
JsonElement je = jsonParser.parse(in);
if(je instanceof JsonPrimitive) {
value = new Value();
value.nameArr = new String[1];
value.nameArr[0] = ((JsonPrimitive)je).getAsString();
} else if (je instanceof JsonArray) {
JsonArray jsonArr = (JsonArray)je;
value = new Value();
value.nameArr = new String[jsonArr.size()];
for (int i = 0; i < jsonArr.size(); i++) {
JsonObject jo = (JsonObject)jsonArr.get(i);
value.nameArr[i] = jo.get("name").getAsString();
}
}
return value;
}
@Override
public void write(JsonWriter out, Value value) throws IOException {
if (value != null) {
if (value.nameArr != null && value.nameArr.length > 0) {
if (value.nameArr.length == 1) {
out.value(value.nameArr[0]);
} else if (value.nameArr.length > 1) {
out.beginArray();
for (String nameVal : value.nameArr) {
out.beginObject();
out.name("name").value(nameVal);
out.endObject();
}
out.endArray();
}
}
}
}
}
Your POJO: 您的POJO:
public class Item {
private String op;
private String path;
private Value value;
// TODO: Add Getters/Setters
}
public class Value {
private String[] nameArr;
// TODO: Add Getters/Setters
}
Test: 测试:
Note: I've used similar json string of yours. 注意:我使用了类似的json字符串。 Difference is second array item holds 2 values.
区别在于第二个数组项包含2个值。 In this test you'll see that deserialization will be done and
itemArr
will be filled correctly. 在此测试中,您将看到反序列化将完成并且
itemArr
将被正确填充。 Also after serialization of itemArr
to json string, you'll see that the result is same with incoming test json string value. 同样,在将
itemArr
序列化为json字符串之后,您itemArr
看到结果与传入的测试json字符串值相同。
String json = "[{\"op\":\"replace\",\"path\":\"/path1\",\"value\":\"val1\"},{\"op\":\"replace\",\"path\":\"/path2\",\"value\":[{\"name\":\"val2\"},{\"name\":\"val3\"}]}]";
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(Value.class, new ValueTypeAdapter());
Gson gson = gsonBuilder.create();
Item[] itemArr = gson.fromJson(json, Item[].class);
String serialized = gson.toJson(itemArr);
System.out.println("serialized:" + serialized);
// NOTE: serialized output is same with incoming test value
You can read more about TypeAdapters from this site . 您可以从该站点阅读有关TypeAdapters的更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.