java - 如何从嵌套的 JSON 中获取数据?
问题描述
这是我的代码:
public class Parser {
private static final String PATH = "try.json";
public static void main(String[] args) throws IOException, ParseException {
String req = FileUtils.readFileToString(new File(PATH), StandardCharsets.UTF_8);
Bean data = new Gson().fromJson(req, Bean.class);
public class Bean{
private List<Data> data;
public List<Data> getData() {
return data;
}
public class Data {
private List<String> urls;
private String name;
private String type;
private String picture;
public List<String> getUrls() {
return urls;
}
public String getName() {
return name;
}
public String getType() {
return type;
}
public String getPicture() {
return picture;
}
}
这是虚拟JSON:
{
"data": [
{
"urls": [
"https://google.com",
"https://googl.com"
],
"name": "Google",
"type": "corp",
"picture": "https://google.img"
},
{
"urls": [
"https://yandex.ru"
],
"name": "Yandex",
"type": "corp",
"picture": "https://yandex.jpg"
}
]
}
所以这里的主要任务 - 从 json 中创建带有“name”字段和“urls”ArrayList 的单个对象,然后我将它们添加到另一个列表中并按索引获取信息。但我就是不明白,我怎么能做到这一点。例如,对于第一个对象的响应可能应该如下所示:
Google
"https://google.com",
"https://googl.com"
解决方案
好吧,如果您不强制使用 GSON,您可以使用 Jackson。
通过使用@JsonProperty
注释,您可以获得嵌套字段,@JsonAlias
如果需要,您还可以添加一些别名。
这是一个简短的例子:
import com.fasterxml.jackson.annotation.JsonProperty;
public class Data {
private List<String> urls;
private String name;
private String type;
private String picture;
public List<String> getUrls() {
return urls;
}
public String getName() {
return name;
}
public String getType() {
return type;
}
public String getPicture() {
return picture;
}
}
@JsonProperty("ParentObject") //Here it looks like the parent object is an element of the array Data, so i would treat you Json object as a list of objects and iterate over each.
private void getFieldNestedValues(Map<String, String> field) {
urls = field.get("nestedField1"); //nestedField1 would be urls in your case
name = field.get("nestedField2"); // nestedField2 could be the name for instance
}
我很确定你可以对 Gson 做同样的事情,希望这个示例能给你一个想法,你可以如何让它工作。
编辑:忘了提到我会使用ObjectMapper
杰克逊库中的一个来解析和构建对象:
JsonNode rootNode = objectMapperProvider.buildSureObjectMapper().readTree(jsonBody);
YourObject yourObject= objectMapperProvider.buildSureObjectMapper().readValue(rootNode.toString(), YourObject.class);
例如,使用自定义 ObjectMapper:
public ObjectMapper buildSureObjectMapper() {
return new ObjectMapper()
.registerModule(new ParameterNamesModule())
.registerModule(new Jdk8Module())
.registerModule(new JavaTimeModule())
.registerModule(new GuavaModule())
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true)
.setSerializationInclusion(NON_ABSENT);
}
推荐阅读
- flutter - Flutter 录音 onProgress 流返回 null
- r - 在 R 中安装 keras:警告:pip 配置了需要 TLS/SSL 的位置,但是 Python 中的 ssl 模块不可用
- reactjs - 如何在单击时向 reactjs 组件添加声音
- php - 如何在WordPress的functions.php中同时使用函数和HTML
- javascript - 如何模拟 MongoDB 客户端事件监听器?
- python - Heroku 无法识别已安装的软件包
- python - pandas_datareader 的结果顺序不正确
- python - Numpy 无法正确计算图像中的像素
- c++ - 如何定义模板类的内部类
- spring - Spring Boot 日志记录 - log4j2 日志记录不起作用?