[英]Creating nested JSON objects with different formats in Java with GSON
I'm 100% certain this has been asked a million times already, but I'm really unsure how to properly approach this.我 100% 确定这已经被问了一百万次了,但我真的不确定如何正确处理这个问题。 I haven't done a lot with JSON or serializing it yet.我还没有对 JSON 或序列化它做很多事情。
Basically, this is what I want to create using GSON:基本上,这就是我想使用 GSON 创建的:
{
"wrapper" : [
{
"content": "loremipsum",
"positions": [0,3]
},
{
"content": "foobar",
"positions": [7]
},
{
"content": "helloworld"
}
]
}
Breaking it down, we've got a field for an array, containing objects which in themselves contains two fields, one of which maps to a string and the other to yet another array that can contain an unknown amount of integers or can be missing entirely.分解它,我们有一个数组字段,其中包含对象本身包含两个字段,其中一个映射到一个字符串,另一个映射到另一个数组,该数组可以包含未知数量的整数或可能完全丢失.
I can't even begin to imagine how to get this result with GSON.我什至无法想象如何用 GSON 得到这个结果。 So far my idea would be to have everything be in a Map<String, List<Map<String, Object>>>
beast and convert it, but that Object bothers me because it could either be a String or a List in this particular case.到目前为止,我的想法是将所有内容都放在Map<String, List<Map<String, Object>>>
野兽中并对其进行转换,但是 Object 困扰着我,因为在这种特殊情况下它可能是字符串或列表. There could be casts, but that sounds like a stupidy complex thing for something that would look easier if I even just manually typed it in a String.format() or similar.可能会有演员表,但这听起来像是一件愚蠢的复杂事情,如果我什至只是手动输入 String.format() 或类似的东西,看起来会更容易。
Isn't there a simpler way of handling this stuff?难道没有更简单的方法来处理这些东西吗?
I would go with creating some POJO class for your Data:我会 go 为您的数据创建一些 POJO class :
class MyData {
private List<Data> wrapper;
//getters setters constructors
}
class Data {
private String content;
private List<Integer> positions;
//getters setters constructors
}
And then for deserializing it:然后反序列化它:
Gson gson = new Gson();
String json = //json here
MyData myData = gson.fromJson(myJson, MyData.class);
And for serializing:对于序列化:
MyData myData = ...
String json = gson.toJson(myData);
Another way could be parsing this structure using JsonParser
and access its elements:另一种方法是使用JsonParser
解析这个结构并访问它的元素:
JsonParser jsonParser = new JsonParser();
JsonElement jsonElement = jsonParser.parse(json);
JsonElement wrapperElement = jsonElement.getAsJsonObject().get("wrapper"); //access wrapper
JsonArray array = wrapperElement.getAsJsonArray(); // access array
It is better to use Pojo for formatting.最好使用 Pojo 进行格式化。
class Wrapper{
private List<Data> data;
// geters, seters
}
class Data{
private String content;
private List<Integer> positions;
// geters, seters
}
For deserialization/serealization you can use Jackson对于反序列化/序列化,您可以使用Jackson
ObjectMapper mapper = new ObjectMapper();
Wrapper wriper = mapper.readValue(dataString, Wrapper.class);
String jsonString = mapper.writeValueAsString(wriper);
Gson gson = new Gson();
Wrapper wriper = gson.fromJson(dataString, Wrapper.class);
String json = gson.toJson(wriper);
you can use gson JsonElement as the contianer object that will contain JsonElements like JsonObject or JsonArray您可以使用 gson JsonElement 作为 contianer object ,它将包含 JsonElements,如 JsonObject 或 JsonArray
https://static.javadoc.io/com.google.code.gson/gson/2.6.2/com/google/gson/JsonElement.html https://static.javadoc.io/com.google.code.gson/gson/2.6.2/com/google/gson/JsonElement.html
Without POJO, this seems to solve the nested JSON solution using Gson没有 POJO,这似乎解决了使用 Gson 的嵌套 JSON 解决方案
package com.test;
import com.google.gson.JsonObject;
public class Main {
public static void main(String[] args) {
JsonObject ex3 = new JsonObject();
ex3.addProperty("example3", 30);
JsonObject ex2 = new JsonObject();
ex2.add("example2", ex3.getAsJsonObject());
JsonObject ex1 = new JsonObject();
ex1.add("example1", ex2.getAsJsonObject());
System.out.println(ex1.toString());
}
}
The output is: output 是:
{"example1":{"example2":{"example3":30}}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.