用Java解析JSON数据-卡在id的里面

时间:2019-03-03 19:03:55

标签: java json parsing jackson

我有以下JSON数据和nations数组。

数据Neighbouring文件中的

JSON指出哪个国家与其他国家有邻居。国家的 id 是指邻国 id

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

-所有数据已使用Jackson

导入

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

{
  "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");

但是不确定从这儿去哪里,我是这种语言的新手。

1 个答案:

答案 0 :(得分:1)

如果您已经使用Jackson,则最好创建POJO并将JSON有效负载反序列化为给定的POJO结构。您的课程如下所示:

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 +
                '}';
    }
}