使用 Gson 解析后,将嵌套的 JSON 数组插入 SQLite
Insert a nested JSON array into SQLite after parsing with Gson
用 Gson
解析嵌套的 JSON 数组后,我现在需要将结果插入 SQLite
。我尝试在不使用 Gson
进行解析时按原样插入,但这没有用。我寻找方法来做到这一点,但找不到解决方案。
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;
}
如果我不使用 Gson,我会写这行:
JSONObject countryObject = countriesList.getJSONObject(country);
编辑
来自 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
}
如果没有更多代码,很难知道您的问题出在哪里。您正在进行两种不同的操作:
- 解组 json
- 在 SQLite 中持久化数据。
使用 Gson
解组 json
在 for 循环中,您能否记录每个国家/地区以查看您正在获得一个设置了所有字段的有效对象?您很可能需要自己创建一个工厂。 Country 是否有需要通过 RuntimeTypeAdapterFactory 注册的子类型?也许像
final RuntimeTypeAdapterFactory<City> typeFactory = RuntimeTypeAdapterFactory
of(City.class, "MyCity")
.registerSubtype(S...,...)
保存在 SQLite 中
获得有效数据后,您必须像这样按字段进行转换
public static ContentValues getContentValues(Country country) {
ContentValues values = new ContentValues();
values.put(COLUMN_ID, country.getId());
values.put(COLUMN_NAME, country.getName());
...
return values;
}
然后,如果它仍然不起作用,您将需要查看您的 SQLite 架构。
先获取 属性 的 Country,然后将其放入内容值中并插入。
List<Country> countries = gson.fromJson(jsonString, new TypeToken<List<Country>>(){}.getType());
for(Country country : countries) {
String name = country.getName();
String capital = country.getCapital();
String region = country.getRegion();
String currencies = gson.toJson(country.getCurrencies());
...
ContentValues insertValues = new ContentValues();
insertValues.put("name", name)
insertValues.put("capital", capital);
insertValues.put("region", region);
insertValues.put("currencies", currencies);
...
long res = db.insert(TABLE_NAME, null, insertValues);
}
用 Gson
解析嵌套的 JSON 数组后,我现在需要将结果插入 SQLite
。我尝试在不使用 Gson
进行解析时按原样插入,但这没有用。我寻找方法来做到这一点,但找不到解决方案。
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;
}
如果我不使用 Gson,我会写这行:
JSONObject countryObject = countriesList.getJSONObject(country);
编辑
来自 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
}
如果没有更多代码,很难知道您的问题出在哪里。您正在进行两种不同的操作:
- 解组 json
- 在 SQLite 中持久化数据。
使用 Gson
解组 json在 for 循环中,您能否记录每个国家/地区以查看您正在获得一个设置了所有字段的有效对象?您很可能需要自己创建一个工厂。 Country 是否有需要通过 RuntimeTypeAdapterFactory 注册的子类型?也许像
final RuntimeTypeAdapterFactory<City> typeFactory = RuntimeTypeAdapterFactory
of(City.class, "MyCity")
.registerSubtype(S...,...)
保存在 SQLite 中
获得有效数据后,您必须像这样按字段进行转换
public static ContentValues getContentValues(Country country) {
ContentValues values = new ContentValues();
values.put(COLUMN_ID, country.getId());
values.put(COLUMN_NAME, country.getName());
...
return values;
}
然后,如果它仍然不起作用,您将需要查看您的 SQLite 架构。
先获取 属性 的 Country,然后将其放入内容值中并插入。
List<Country> countries = gson.fromJson(jsonString, new TypeToken<List<Country>>(){}.getType());
for(Country country : countries) {
String name = country.getName();
String capital = country.getCapital();
String region = country.getRegion();
String currencies = gson.toJson(country.getCurrencies());
...
ContentValues insertValues = new ContentValues();
insertValues.put("name", name)
insertValues.put("capital", capital);
insertValues.put("region", region);
insertValues.put("currencies", currencies);
...
long res = db.insert(TABLE_NAME, null, insertValues);
}