在Java中解析JSON数据-使用id';s
我有以下在Java中解析JSON数据-使用id';s,java,json,parsing,jackson,Java,Json,Parsing,Jackson,我有以下JSON数据和国家的数组 数据JSON文件中的邻国表示哪些国家与其他国家有邻国。国家的id指邻国id 也就是说,俄罗斯与乌克兰相邻,波兰与乌克兰相邻,乌克兰与波兰和俄罗斯相邻,等等 -所有数据都已使用Jackson导入 如何列出每个国家的邻国? 即,我如何才能将乌克兰与俄罗斯和波兰列为邻国 { "nations" : [{ "id" : 1, "name" : "Russia", "population" : 1000000000, "
JSON
数据和国家的数组
数据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");
但是我不知道从这里该往哪里走,我对这种语言非常陌生。如果您已经使用它,那么创建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 + '\'' +
'}';
}
}
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 +
'}';
}
}
有关更多信息,请阅读:
更改
JSON
后的模型可能如下所示:
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 + '\'' +
'}';
}
}
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 +
'}';
}
}
抽象类区域{
受保护的int-id;
受保护的字符串名称;
受保护的国际人口;
公共int getId(){
返回id;
}
公共无效集合id(内部id){
this.id=id;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共整数getPopulation(){
返回人口;
}
公共人口(整数人口){
这个。人口=人口;
}
}
阶级国家扩展面积{
私人名单城市;
公共列表(城市){
回归城市;
}
公共城市(列出城市){
这个。城市=城市;
}
@凌驾
公共字符串toString(){
返回“国家{”+
“id=”+id+
“,name=”“+name+”\“””+
“,人口=”+人口+
“,cities=“+城市+
'}';
}
}
阶级城市扩展面积{
@凌驾
公共字符串toString(){
返回“城市{”+
“id=”+id+
“,name=”“+name+”\“””+
“,人口=”+人口+
'}';
}
}
为什么不将此JSON解析为POJO?@johnD,编辑后看起来像是JSON
与POJO
模型不匹配。JSON
有效吗?检查它使用。如果您无法解决此问题,请编辑您的问题并添加新的JSON
有效负载和类。@johnD,Nations
类的外观应该相同。