gpt4 book ai didi

java - 在 Java 中解析 JSON 数据 - 受 id 困扰

转载 作者:行者123 更新时间:2023-12-02 10:11:30 24 4
gpt4 key购买 nike

我有以下 JSON 数据,其中包含一个 nations 数组。

数据 JSON 文件中的

Neighbouring 指示哪些国家与其他国家有邻居。国家的id指的是邻国的id

即。俄罗斯与乌克兰相邻,波兰与乌克兰相邻,乌克兰与波兰和俄罗斯相邻,等等。

-所有数据均已使用 Jackson 导入

如何列出每个国家的邻国?IE。如何列出乌克兰俄罗斯和波兰作为邻居?

{
"nations" : [{
"id" : 1,
"name" : "Russia",
"population" : 1000000000,
"cities" : [{
"id" : 222,
"name" : "Moscow",
"population": 4884333
},{
"id" : 223,
"name" : "Kazan",
"population": 799343
}]
},{

我想,我可以用这一行返回数组[3]:

JSONArray array = value.getJSONObject("neighbouring").getJSONArray("1");

但不知道从这里到哪里去,我对这种语言很陌生。

最佳答案

如果您已经使用Jackson最好创建 POJO 并将 JSON 有效负载反序列化为给定的 POJO 结构。您的类(class)可能如下所示:

class Nations {

private List<Nation> nations;
private Map<Integer, List<Integer>> neighbouring;

public List<Nation> getNations() {
return nations;
}

public void setNations(List<Nation> nations) {
this.nations = nations;
}

public Map<Integer, List<Integer>> getNeighbouring() {
return neighbouring;
}

public void setNeighbouring(Map<Integer, List<Integer>> neighbouring) {
this.neighbouring = neighbouring;
}

@Override
public String toString() {
return "Nations{" +
"nations=" + nations +
", neighbouring=" + neighbouring +
'}';
}
}

class Nation {
private int id;
private String name;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Override
public String toString() {
return "Nation{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}

现在,当我们有了模型时,我们可以尝试解析并打印每个国家/地区的邻居:

import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class JsonApp {

public static void main(String[] args) throws Exception {
File jsonFile = new File("./resource/test.json").getAbsoluteFile();

ObjectMapper mapper = new ObjectMapper();
Nations nations = mapper.readValue(jsonFile, Nations.class);

// To make lookup fast create map id -> name
Map<Integer, String> id2Name = new HashMap<>();
nations.getNations().forEach(nation -> id2Name.put(nation.getId(), nation.getName()));

Map<Integer, List<Integer>> neighbouring = nations.getNeighbouring();
nations.getNations().forEach(nation -> {
List<String> neighbours = neighbouring.get(nation.getId())
.stream().map(id2Name::get).collect(Collectors.toList());
System.out.println(nation.getName() + " => " + neighbours);
});
}
}

上面的代码打印:

Russia => [Ukraine]
Poland => [Ukraine]
Ukraine => [Russia, Poland]

欲了解更多信息,请阅读:

编辑
更改 JSON 后的模型可能如下所示:

abstract class Area {

protected int id;
protected String name;
protected int population;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getPopulation() {
return population;
}

public void setPopulation(int population) {
this.population = population;
}
}

class Nation extends Area {

private List<City> cities;

public List<City> getCities() {
return cities;
}

public void setCities(List<City> cities) {
this.cities = cities;
}

@Override
public String toString() {
return "Nation{" +
"id=" + id +
", name='" + name + '\'' +
", population=" + population +
", cities=" + cities +
'}';
}
}

class City extends Area {

@Override
public String toString() {
return "City{" +
"id=" + id +
", name='" + name + '\'' +
", population=" + population +
'}';
}
}

关于java - 在 Java 中解析 JSON 数据 - 受 id 困扰,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54972581/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com