Android 在使用Gson进行解析后,将嵌套的JSON数组插入到SQLite中

Android 在使用Gson进行解析后,将嵌套的JSON数组插入到SQLite中,android,json,sqlite,gson,Android,Json,Sqlite,Gson,在使用Gson解析嵌套的JSON数组之后,我现在需要将结果插入SQLite。我尝试在不使用Gson进行解析时按原样插入,但不起作用。我想办法做到这一点,但找不到解决办法 JSON解析: Gson gson = new Gson(); Type listType = new TypeToken<List<Country>>(){}.getType(); List<Country> countriesList = gson.fromJson(jsonString,

在使用
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;
}
编辑

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
}
public class Currency implements Parcelable {

    private String name;
    private String symbol;

//getters, setters, toString() and Parcelable methods
}
public class Language implements Parcelable {

    private String name;
    private String nativeName;

//getters, setters, toString() and Parcelable methods
}
模型类语言.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
}
public class Currency implements Parcelable {

    private String name;
    private String symbol;

//getters, setters, toString() and Parcelable methods
}
public class Language implements Parcelable {

    private String name;
    private String nativeName;

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

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

  • 解组json
  • 在SQLite中持久化数据 用Gson解组json

    在for循环中,您是否可以记录每个国家/地区,以查看您是否获得了设置了所有字段的有效对象?很可能你需要自己创建一个工厂。国家/地区是否有您需要通过RuntimeTypeAdapterFactory注册的子类型?也许像

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

    然后,如果它仍然不起作用,您将需要查看您的SQLite模式

    首先获取国家的属性,然后将其放入内容值并插入

    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); 
    
    }
    
    List countries=gson.fromJson(jsonString,new-TypeToken(){}.getType());
    (国家:国家){
    字符串名称=country.getName();
    字符串capital=country.getCapital();
    字符串region=country.getRegion();
    字符串currences=gson.toJson(country.getcurrences());
    ...
    ContentValues insertValues=新ContentValues();
    insertValues.put(“name”,name)
    insertValues.put(“大写”,大写);
    insertValues.put(“区域”,区域);
    插入值。看跌期权(“货币”,货币);
    ...
    long res=db.insert(表名称,null,insertValues);
    }
    
    您的DB结构是什么?添加JSON示例会提供足够的infomarion?这在注释中太多了,因此我将其作为响应键入。也许这就是你所需要的。如果没有,我可以根据你得到的距离来编辑这个回复。你没有从中得到任何想法吗?似乎是这样。谢谢。如果这对你有帮助,那么接受答案并按upvote。谢谢你知道如何插入嵌套对象吗?请发布详细信息并让我知道链接