【问题标题】:Insert a nested JSON array into SQLite after parsing with Gson使用 Gson 解析后将嵌套的 JSON 数组插入 SQLite
【发布时间】:2020-03-04 11:48:12
【问题描述】:

使用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"
   },

我写的模型类只针对我需要的变量对象和数组。

模型类 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
}

【问题讨论】:

  • 你的数据库结构是什么?
  • 添加 JSON 样本可以提供足够的信息?

标签: android json sqlite gson


【解决方案1】:

先获取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); 

}

【讨论】:

  • 你没有从中得到任何想法吗?
  • 好像是这样。谢谢。
  • 如果对您有帮助,请接受答案并按upvote。谢谢
  • 你知道如何插入嵌套对象吗?
  • 详细发帖告诉我链接
【解决方案2】:

如果没有更多代码,很难知道问题出在哪里。您正在执行两种不同的操作:

  1. 解组 json
  2. 在 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 架构。

【讨论】:

  • 这在评论中说得太多了,因此我将其输入为回复。所以也许这就是你所需要的。如果没有,我可以根据您的进展情况编辑此回复。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多