解析包含不同对象列表的 JSON 文件

Posted

技术标签:

【中文标题】解析包含不同对象列表的 JSON 文件【英文标题】:Parse JSON file containing different lists of objects 【发布时间】:2018-08-26 23:28:11 【问题描述】:

我有一个 JSON 文件,它是我的 android 应用程序的 Sqlite DB 的导出文件。它包含不同类型的不同列表


  "territories": [
    
      "created": "Mar 17, 2018 8:30:28 PM",
      "id": 1,
      "name": "tt",
      "parentId": 0
    ,
    
      "created": "Mar 17, 2018 8:45:58 PM",
      "id": 2,
      "name": "aa",
      "parentId": 0
    
  ],
  "streets": [
    
      "created": "Mar 18, 2018 7:10:23 AM",
      "id": 1,
      "name": "bb",
      "parentId": 2
    
  ],
  "buildings": [
    
      "created": "Mar 18, 2018 7:10:28 AM",
      "id": 1,
      "name": "cc",
      "parentId": 1
    
  ],
  "flats": [
    
      "created": "Mar 18, 2018 7:10:36 AM",
      "id": 1,
      "name": "dd",
      "parentId": 1
    
  ],
  "vists": [
    
      "buildingId": 1,
      "categoryId": 6,
      "dateTime": "Mar 18, 2018 7:10:00 AM",
      "description": "Xxxxx",
      "flatId": 1,
      "id": 1,
      "streetId": 1,
      "territoryId": 2
    
  ]

我知道如何解析包含具有相同对象的单个列表的 JSON 文件:

Gson gson = new Gson();
List<DbBaseType> territoryList = null;
try 
    territoryList = gson.fromJson(new FileReader("path/my.export"),
            new TypeToken<List<DbBaseType>>().getType());
 catch (FileNotFoundException e) 
    //...

但是如何解析包含具有不同对象的不同列表的文件呢?

【问题讨论】:

只要结构相同,就可以使用Map&lt;String, List&lt;Pojo&gt;&gt;。虽然不知道如何处理visits 你应该有Map&lt;String,Object[]&gt; 是的,Visits 是另一种类型。这就是问题的重点。 @pskink:我不想导出/导入所有内容。这只是部分数据。 @pskink:好的。我调查一下。谢谢指点。 【参考方案1】:

他们的网站帮助您生成名为的 Java 类 链接:http://www.jsonschema2pojo.org/

这是问题中的响应示例:

-----------------------------------com.example.Building.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Building 

@SerializedName("created")
@Expose
private String created;
@SerializedName("id")
@Expose
private int id;
@SerializedName("name")
@Expose
private String name;
@SerializedName("parentId")
@Expose
private int parentId;

/**
* No args constructor for use in serialization
* 
*/
public Building() 


/**
* 
* @param id
* @param parentId
* @param created
* @param name
*/
public Building(String created, int id, String name, int parentId) 
super();
this.created = created;
this.id = id;
this.name = name;
this.parentId = parentId;


public String getCreated() 
return created;


public void setCreated(String created) 
this.created = created;


public Building withCreated(String created) 
this.created = created;
return this;


public int getId() 
return id;


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


public Building withId(int id) 
this.id = id;
return this;


public String getName() 
return name;


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


public Building withName(String name) 
this.name = name;
return this;


public int getParentId() 
return parentId;


public void setParentId(int parentId) 
this.parentId = parentId;


public Building withParentId(int parentId) 
this.parentId = parentId;
return this;



-----------------------------------com.example.Example.java-----------------------------------

package com.example;

import java.util.ArrayList;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Example 

@SerializedName("territories")
@Expose
private List<Territory> territories = new ArrayList<Territory>();
@SerializedName("streets")
@Expose
private List<Street> streets = new ArrayList<Street>();
@SerializedName("buildings")
@Expose
private List<Building> buildings = new ArrayList<Building>();
@SerializedName("flats")
@Expose
private List<Flat> flats = new ArrayList<Flat>();
@SerializedName("vists")
@Expose
private List<Vist> vists = new ArrayList<Vist>();

/**
* No args constructor for use in serialization
* 
*/
public Example() 


/**
* 
* @param vists
* @param territories
* @param buildings
* @param streets
* @param flats
*/
public Example(List<Territory> territories, List<Street> streets, List<Building> buildings, List<Flat> flats, List<Vist> vists) 
super();
this.territories = territories;
this.streets = streets;
this.buildings = buildings;
this.flats = flats;
this.vists = vists;


public List<Territory> getTerritories() 
return territories;


public void setTerritories(List<Territory> territories) 
this.territories = territories;


public Example withTerritories(List<Territory> territories) 
this.territories = territories;
return this;


public List<Street> getStreets() 
return streets;


public void setStreets(List<Street> streets) 
this.streets = streets;


public Example withStreets(List<Street> streets) 
this.streets = streets;
return this;


public List<Building> getBuildings() 
return buildings;


public void setBuildings(List<Building> buildings) 
this.buildings = buildings;


public Example withBuildings(List<Building> buildings) 
this.buildings = buildings;
return this;


public List<Flat> getFlats() 
return flats;


public void setFlats(List<Flat> flats) 
this.flats = flats;


public Example withFlats(List<Flat> flats) 
this.flats = flats;
return this;


public List<Vist> getVists() 
return vists;


public void setVists(List<Vist> vists) 
this.vists = vists;


public Example withVists(List<Vist> vists) 
this.vists = vists;
return this;



-----------------------------------com.example.Flat.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Flat 

@SerializedName("created")
@Expose
private String created;
@SerializedName("id")
@Expose
private int id;
@SerializedName("name")
@Expose
private String name;
@SerializedName("parentId")
@Expose
private int parentId;

/**
* No args constructor for use in serialization
* 
*/
public Flat() 


/**
* 
* @param id
* @param parentId
* @param created
* @param name
*/
public Flat(String created, int id, String name, int parentId) 
super();
this.created = created;
this.id = id;
this.name = name;
this.parentId = parentId;


public String getCreated() 
return created;


public void setCreated(String created) 
this.created = created;


public Flat withCreated(String created) 
this.created = created;
return this;


public int getId() 
return id;


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


public Flat withId(int id) 
this.id = id;
return this;


public String getName() 
return name;


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


public Flat withName(String name) 
this.name = name;
return this;


public int getParentId() 
return parentId;


public void setParentId(int parentId) 
this.parentId = parentId;


public Flat withParentId(int parentId) 
this.parentId = parentId;
return this;



-----------------------------------com.example.Street.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Street 

@SerializedName("created")
@Expose
private String created;
@SerializedName("id")
@Expose
private int id;
@SerializedName("name")
@Expose
private String name;
@SerializedName("parentId")
@Expose
private int parentId;

/**
* No args constructor for use in serialization
* 
*/
public Street() 


/**
* 
* @param id
* @param parentId
* @param created
* @param name
*/
public Street(String created, int id, String name, int parentId) 
super();
this.created = created;
this.id = id;
this.name = name;
this.parentId = parentId;


public String getCreated() 
return created;


public void setCreated(String created) 
this.created = created;


public Street withCreated(String created) 
this.created = created;
return this;


public int getId() 
return id;


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


public Street withId(int id) 
this.id = id;
return this;


public String getName() 
return name;


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


public Street withName(String name) 
this.name = name;
return this;


public int getParentId() 
return parentId;


public void setParentId(int parentId) 
this.parentId = parentId;


public Street withParentId(int parentId) 
this.parentId = parentId;
return this;



-----------------------------------com.example.Territory.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Territory 

@SerializedName("created")
@Expose
private String created;
@SerializedName("id")
@Expose
private int id;
@SerializedName("name")
@Expose
private String name;
@SerializedName("parentId")
@Expose
private int parentId;

/**
* No args constructor for use in serialization
* 
*/
public Territory() 


/**
* 
* @param id
* @param parentId
* @param created
* @param name
*/
public Territory(String created, int id, String name, int parentId) 
super();
this.created = created;
this.id = id;
this.name = name;
this.parentId = parentId;


public String getCreated() 
return created;


public void setCreated(String created) 
this.created = created;


public Territory withCreated(String created) 
this.created = created;
return this;


public int getId() 
return id;


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


public Territory withId(int id) 
this.id = id;
return this;


public String getName() 
return name;


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


public Territory withName(String name) 
this.name = name;
return this;


public int getParentId() 
return parentId;


public void setParentId(int parentId) 
this.parentId = parentId;


public Territory withParentId(int parentId) 
this.parentId = parentId;
return this;



-----------------------------------com.example.Vist.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Vist 

@SerializedName("buildingId")
@Expose
private int buildingId;
@SerializedName("categoryId")
@Expose
private int categoryId;
@SerializedName("dateTime")
@Expose
private String dateTime;
@SerializedName("description")
@Expose
private String description;
@SerializedName("flatId")
@Expose
private int flatId;
@SerializedName("id")
@Expose
private int id;
@SerializedName("streetId")
@Expose
private int streetId;
@SerializedName("territoryId")
@Expose
private int territoryId;

/**
* No args constructor for use in serialization
* 
*/
public Vist() 


/**
* 
* @param id
* @param territoryId
* @param flatId
* @param streetId
* @param dateTime
* @param description
* @param categoryId
* @param buildingId
*/
public Vist(int buildingId, int categoryId, String dateTime, String description, int flatId, int id, int streetId, int territoryId) 
super();
this.buildingId = buildingId;
this.categoryId = categoryId;
this.dateTime = dateTime;
this.description = description;
this.flatId = flatId;
this.id = id;
this.streetId = streetId;
this.territoryId = territoryId;


public int getBuildingId() 
return buildingId;


public void setBuildingId(int buildingId) 
this.buildingId = buildingId;


public Vist withBuildingId(int buildingId) 
this.buildingId = buildingId;
return this;


public int getCategoryId() 
return categoryId;


public void setCategoryId(int categoryId) 
this.categoryId = categoryId;


public Vist withCategoryId(int categoryId) 
this.categoryId = categoryId;
return this;


public String getDateTime() 
return dateTime;


public void setDateTime(String dateTime) 
this.dateTime = dateTime;


public Vist withDateTime(String dateTime) 
this.dateTime = dateTime;
return this;


public String getDescription() 
return description;


public void setDescription(String description) 
this.description = description;


public Vist withDescription(String description) 
this.description = description;
return this;


public int getFlatId() 
return flatId;


public void setFlatId(int flatId) 
this.flatId = flatId;


public Vist withFlatId(int flatId) 
this.flatId = flatId;
return this;


public int getId() 
return id;


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


public Vist withId(int id) 
this.id = id;
return this;


public int getStreetId() 
return streetId;


public void setStreetId(int streetId) 
this.streetId = streetId;


public Vist withStreetId(int streetId) 
this.streetId = streetId;
return this;


public int getTerritoryId() 
return territoryId;


public void setTerritoryId(int territoryId) 
this.territoryId = territoryId;


public Vist withTerritoryId(int territoryId) 
this.territoryId = territoryId;
return this;



点击此链接了解如何从设备读取文件: ***.com/a/14377185/6049350

并使用此代码从 json 文件中解析字符串。

Gson gson = new Gson();
surahModels = gson.fromJson(jsonStringFromFile), new TypeToken<List<Example>>() .getType());

【讨论】:

我已经有了这些对象。我需要将 JSON 文件中的数据填充到对象中。 @ju 是项目里面的文件还是设备里面的文件? 是的,在设备上。 点击此链接了解如何从文件中读取:***.com/a/14377185/6049350 @juergend 查看答案中的类并使用示例类进行解析

以上是关于解析包含不同对象列表的 JSON 文件的主要内容,如果未能解决你的问题,请参考以下文章

如何解析包含对象列表的 JSON

如何使用 Newtonsoft.Json 将包含数组数组的 json 对象解析为 C# 中的对象列表?

如何在没有外部库的情况下解析包含对象列表的对象列表

Python 处理包含对象列表的大型 JSON 文件

将包含许多嵌套对象的JSON文件保存到列表中

当列表的各个项目使用Retrofit和Gson的格式不同时,如何解析json列表?