将嵌套的 JSON 解析并保存到 SQLite 中,然后检索它
Parsing and saving a nested JSON into SQLite, and then retrieving it
编辑
我设法只插入了外部 JSON 对象。找不到有关如何插入嵌套对象的信息。每个对象中都有原始字段。这些对象可以在下面的 JSON 示例中看到:"languages" 和 "currencies"。我也想知道如何处理外部 JSON 对象中的 "latlng" 数组,但最好一次处理这些问题。
我得到的用于在外部 JSON 对象中插入原始值的代码:
Gson gson = new Gson();
Type listType = new TypeToken<List<Country>>(){}.getType();
List<Country> countriesList = gson.fromJson(jsonString, listType);
for(Country country : countriesList) {
ContentValues insertValues = new ContentValues();
insertValues.put(Country.NAME, country.getName());
//more insertions
}
原题
我用 GSON 解析了一个嵌套的 JSON(可能不正确,因为这是我第一次)。现在我试图将它插入到 SQLite 中。找不到接下来需要写的内容。当我为插入编写 for 循环时,出现错误 cannot find symbol class Country
。无法在网上找到相关指导,所以我希望任何人都可以帮助推进。
这就是问题开始的地方:
Country[] countriesArray = gson.fromJson(jsonString, Country[].class);
for (int i = 0; i < countriesArray.length(); i++) {
...
}
countriesArray.length()
被标记为错误:找不到符号 class 国家。
一个对象来自 JSON:
[
{
"name":"Afghanistan",
"topLevelDomain":[
".af"
],
"callingCodes":[
"93"
],
"capital":"Kabul",
"region":"Asia",
"subregion":"Southern Asia",
"population":27657145,
"latlng":[
33.0,
65.0
],
"demonym":"Afghan",
"area":652230.0,
"gini":27.8,
"timezones":[
"UTC+04:30"
],
"nativeName":"افغانستان",
"numericCode":"004",
"currencies":[
{
"name":"Afghan afghani",
"symbol":"؋"
}
],
"languages":[
{
"name":"Pashto",
"nativeName":"پښتو"
},
{
"name":"Uzbek",
"nativeName":"Oʻzbek"
},
{
"name":"Turkmen",
"nativeName":"Türkmen"
}
],
"translations":{
"de":"Afghanistan",
},
"flag":"https://restcountries.eu/data/afg.svg",
"cioc":"AFG"
},
我写的模型 classes 仅适用于我需要的变量对象和数组。
型号classCountry.Java
public class Country implements Parcelable {
private String name;
private String capital;
private String region;
private String subregion;
private int population;
private List<Double> latlng = new ArrayList<Double>();
private double area;
private double gini;
private List<String> timezones = new ArrayList<String>();
private List<Currency> currencies = new ArrayList<Currency>();
private List<Language> languages = new ArrayList<Language>();
private String flag;
public Country() {}
//getters, setters, toString() and Parcelable methods
}
型号classCurrency.Java
public class Currency implements Parcelable {
private String name;
private String symbol;
//getters, setters, toString() and Parcelable methods
}
型号classLanguage.Java
public class Language implements Parcelable {
private String name;
private String nativeName;
//getters, setters, toString() and Parcelable methods
}
创建一个 typeToken 并将其传递给 fromJson 方法,如下所示
Type listType = new TypeToken<List<Country>>(){}.getType();
List<Country> countryList = gson.fromJson(jsonArray.toString(), listType);
使用 Gson TypeToken
在 JSON
解析期间设置 return 对象类型
List<Country> countries = gson.fromJson(jsonString, new TypeToken<List<Country>>(){}.getType());
for(Country country : countries) {
//Do your DB operation here
}
编辑
我设法只插入了外部 JSON 对象。找不到有关如何插入嵌套对象的信息。每个对象中都有原始字段。这些对象可以在下面的 JSON 示例中看到:"languages" 和 "currencies"。我也想知道如何处理外部 JSON 对象中的 "latlng" 数组,但最好一次处理这些问题。
我得到的用于在外部 JSON 对象中插入原始值的代码:
Gson gson = new Gson();
Type listType = new TypeToken<List<Country>>(){}.getType();
List<Country> countriesList = gson.fromJson(jsonString, listType);
for(Country country : countriesList) {
ContentValues insertValues = new ContentValues();
insertValues.put(Country.NAME, country.getName());
//more insertions
}
原题
我用 GSON 解析了一个嵌套的 JSON(可能不正确,因为这是我第一次)。现在我试图将它插入到 SQLite 中。找不到接下来需要写的内容。当我为插入编写 for 循环时,出现错误 cannot find symbol class Country
。无法在网上找到相关指导,所以我希望任何人都可以帮助推进。
这就是问题开始的地方:
Country[] countriesArray = gson.fromJson(jsonString, Country[].class);
for (int i = 0; i < countriesArray.length(); i++) {
...
}
countriesArray.length()
被标记为错误:找不到符号 class 国家。
一个对象来自 JSON:
[
{
"name":"Afghanistan",
"topLevelDomain":[
".af"
],
"callingCodes":[
"93"
],
"capital":"Kabul",
"region":"Asia",
"subregion":"Southern Asia",
"population":27657145,
"latlng":[
33.0,
65.0
],
"demonym":"Afghan",
"area":652230.0,
"gini":27.8,
"timezones":[
"UTC+04:30"
],
"nativeName":"افغانستان",
"numericCode":"004",
"currencies":[
{
"name":"Afghan afghani",
"symbol":"؋"
}
],
"languages":[
{
"name":"Pashto",
"nativeName":"پښتو"
},
{
"name":"Uzbek",
"nativeName":"Oʻzbek"
},
{
"name":"Turkmen",
"nativeName":"Türkmen"
}
],
"translations":{
"de":"Afghanistan",
},
"flag":"https://restcountries.eu/data/afg.svg",
"cioc":"AFG"
},
我写的模型 classes 仅适用于我需要的变量对象和数组。
型号classCountry.Java
public class Country implements Parcelable {
private String name;
private String capital;
private String region;
private String subregion;
private int population;
private List<Double> latlng = new ArrayList<Double>();
private double area;
private double gini;
private List<String> timezones = new ArrayList<String>();
private List<Currency> currencies = new ArrayList<Currency>();
private List<Language> languages = new ArrayList<Language>();
private String flag;
public Country() {}
//getters, setters, toString() and Parcelable methods
}
型号classCurrency.Java
public class Currency implements Parcelable {
private String name;
private String symbol;
//getters, setters, toString() and Parcelable methods
}
型号classLanguage.Java
public class Language implements Parcelable {
private String name;
private String nativeName;
//getters, setters, toString() and Parcelable methods
}
创建一个 typeToken 并将其传递给 fromJson 方法,如下所示
Type listType = new TypeToken<List<Country>>(){}.getType();
List<Country> countryList = gson.fromJson(jsonArray.toString(), listType);
使用 Gson TypeToken
在 JSON
解析期间设置 return 对象类型
List<Country> countries = gson.fromJson(jsonString, new TypeToken<List<Country>>(){}.getType());
for(Country country : countries) {
//Do your DB operation here
}