首页 > 解决方案 > 使用 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
}

标签: androidjsonsqlitegson

解决方案


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

}

推荐阅读