android - 使用 Gson 解析后将嵌套的 JSON 数组插入 SQLite
问题描述
用 解析嵌套的 JSON 数组后Gson
,我现在需要将结果插入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"
},
我编写的模型类仅适用于我需要的变量对象和数组。
模型类 Country.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
}
模型类 Currency.Java
public class Currency implements Parcelable {
private String name;
private String symbol;
//getters, setters, toString() and Parcelable methods
}
模型类 Language.Java
public class Language implements Parcelable {
private String name;
private String nativeName;
//getters, setters, toString() and Parcelable methods
}
解决方案
首先获取 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);
}
推荐阅读
- ios - 用 KVC 绕过 ObjC 到 Swift 的桥接?
- c# - ASP.NET Core Windows 服务中的 Serilog 无法将文件写入本地系统
- java - 如何通过 Java 运行需要管理员权限的 PowerShell 脚本
- python - 来自 bs4 的 get_text() 对于 span 标签是否不同?无法删除跨度标签
- mysql - 单个查询 SQL 中的多个计数
- sql - 在函数 sql teradata 上创建主键 ROW_NUMBER()
- javascript - 我可以控制如何在 ES6 中创建类吗?
- javascript - 在没有来自地址的情况下通过 html 表单帖子发送电子邮件
- ibm-midrange - 如何更改从 sda 调用 CRTMNU 的默认值
- python - 为什么 os 模块不运行 wget cmd 命令?