简体   繁体   中英

Insert a nested JSON array into SQLite after parsing with Gson

After parsing a nested JSON array with Gson , I now need to insert the result into SQLite . I tried inserting as done when not parsing with Gson , but that didn't work. I looked for ways to do that but couldn't find a solution.

The JSON parsing:

Gson gson = new Gson();
Type listType = new TypeToken<List<Country>>(){}.getType();
List<Country> countriesList = gson.fromJson(jsonString, listType);
for(Country country : countriesList) {
    ContentValues insertValues;
}

If I wasn't using Gson, I would have written the line:

JSONObject countryObject = countriesList.getJSONObject(country);

EDIT

One of the objects from the 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"
   },

The model classes I wrote are only for the variables objects and arrays I needed.

The model class 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
}

The model class Currency.Java

public class Currency implements Parcelable {

    private String name;
    private String symbol;

//getters, setters, toString() and Parcelable methods
}

The model class Language.Java

public class Language implements Parcelable {

    private String name;
    private String nativeName;

//getters, setters, toString() and Parcelable methods
}

It's hard to know where your problem is without more code. You are doing two different operations:

  1. unmarshalling a json
  2. persisting data in SQLite.

Unmarshalling json with Gson

Inside the for-loop, can you log each country to see that you are getting a valid object with all the fields set? It is very much possible that you need to create a factory yourself. Does Country have subtype that you need to register through RuntimeTypeAdapterFactory? Maybe something like

final RuntimeTypeAdapterFactory<City> typeFactory = RuntimeTypeAdapterFactory
  of(City.class, "MyCity")
  .registerSubtype(S...,...)

Save in SQLite

Once you have valid data, then you must convert per field like so

public static ContentValues getContentValues(Country country) {
    ContentValues values = new ContentValues();
    values.put(COLUMN_ID, country.getId());
    values.put(COLUMN_NAME, country.getName());
    ...
    return values;
}

And then, if it still doesn't work, you will need to look at your SQLite schema.

First get property of Country and then put it to content values and insert.

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); 

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM