GSON解析JSON保存到数据库

今天给大家带来的文章为通过Gson解析json数据并快速保存至数据库的文章。我们要保存是json对象数组,本文中的json数组并非从后台获取,为了演示,直接手动构造。

需要保存到数据库的是手机的品牌和型号。所以,我们需要新建一个bean实体类去保存我们的型号和品牌。在这,我先介绍2个工具,一个是Google官方的Gson解析jar包。

名为Gson.jar,这个百度下载就可以了。另外一个是序列化插件Parcelable。在setting---->>>>plugin----->搜索Parcelable。



好了,下面新建我们的bean,取名DeviceModelBean.Java。添加属性后,右键选择Generate--->>>Parcelable,然后快速直接序列化,对bean我们最好养成习惯,先序列化。

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.mero.wyt_register.bean;  
  2.   
  3. import android.os.Parcel;  
  4. import android.os.Parcelable;  
  5.   
  6. /** 
  7.  * Created by chenlei on 2016/10/28. 
  8.  */  
  9.   
  10. public class DeviceModelBean implements Parcelable {  
  11.   
  12.     public String getBrand() {  
  13.         return brand;  
  14.     }  
  15.   
  16.     public void setBrand(String brand) {  
  17.         this.brand = brand;  
  18.     }  
  19.   
  20.     public String getModel() {  
  21.         return model;  
  22.     }  
  23.   
  24.     public void setModel(String model) {  
  25.         this.model = model;  
  26.     }  
  27.   
  28.     public String model;//型号  
  29.     public String brand;//品牌  
  30.     @Override  
  31.     public int describeContents() {  
  32.         return 0;  
  33.     }  
  34.   
  35.     @Override  
  36.     public void writeToParcel(Parcel dest, int flags) {  
  37.         dest.writeString(this.model);  
  38.         dest.writeString(this.brand);  
  39.     }  
  40.   
  41.     protected DeviceModelBean(Parcel in) {  
  42.         this.model = in.readString();  
  43.         this.brand = in.readString();  
  44.     }  
  45.   
  46.     public static final Parcelable.Creator<DeviceModelBean> CREATOR = new Parcelable.Creator<DeviceModelBean>() {  
  47.         @Override  
  48.         public DeviceModelBean createFromParcel(Parcel source) {  
  49.             return new DeviceModelBean(source);  
  50.         }  
  51.   
  52.         @Override  
  53.         public DeviceModelBean[] newArray(int size) {  
  54.             return new DeviceModelBean[size];  
  55.         }  
  56.     };  
  57. }  


接下来,再看看我们的json字符串的内容。这个字符串是我按照预期目的而构造的。

json字符串内容如下:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. String jsonString = "[{\"brand\":\"华为\",\"model\":\"c8818\"},{\"brand\":\"华为\",\"model\":\"Y635\"}," +  
  2.             "{\"brand\":\"华为\",\"model\":\"Y635-CL00\"},{\"brand\":\"华为\",\"model\":\"P8 Lite\"},{\"brand\":\"华为\",\"model\":\"荣耀X2\"}," +  
  3.             "{\"brand\":\"华为\",\"model\":\"荣耀Hol-T00\"},{\"brand\":\"华为\",\"model\":\"荣耀3X畅玩版\"}," +  
  4.             "{\"brand\":\"华为\",\"model\":\"荣耀6\"},{\"brand\":\"华为\",\"model\":\"荣耀4C\"},{\"brand\":\"华为\",\"model\":\"荣耀X3升级版\"}," +  
  5.             "{\"brand\":\"华为\",\"model\":\"C8816\"},{\"brand\":\"华为\",\"model\":\"C8816D\"},{\"brand\":\"华为\",\"model\":\"Mate 7\"},{\"brand\":\"华为\",\"model\":\"荣耀畅玩4C\"}," +  
  6.             "{\"brand\":\"华为\",\"model\":\"荣耀7\"},{\"brand\":\"华为\",\"model\":\"荣耀畅玩4C\"},{\"brand\":\"华为\",\"model\":\"荣耀7\"},{\"brand\":\"华为\",\"model\":\"荣耀4A\"}," +  
  7.             "{\"brand\":\"华为\",\"model\":\"P8\"},{\"brand\":\"华为\",\"model\":\"C2900\"},{\"brand\":\"华为\",\"model\":\"Y320\"}," +  
  8.             "{\"brand\":\"华为\",\"model\":\"C8815\"},{\"brand\":\"华为\",\"model\":\"Mate\"},{\"brand\":\"华为\",\"model\":\"Y600\"}," +  
  9.             "{\"brand\":\"华为\",\"model\":\"荣耀6 Plus\"},{\"brand\":\"华为\",\"model\":\"C8817L\"},{\"brand\":\"华为\",\"model\":\"G5000\"}," +  
  10.             "{\"brand\":\"华为\",\"model\":\"C8817E\"},{\"brand:\":\"华为\",\"model\":\"荣耀6X\"},{\"brand\":\"华为\",\"model\":\"P8 Lite\"}," +  
  11.             "{\"brand\":\"华为\",\"model\":\"Ascend P8\"},{\"brand\":\"华为\",\"model\":\"荣耀畅玩4X\"},{\"brand\":\"华为\",\"model\":\"G629\"},{\"brand\":\"华为\",\"model\":\"G620\"},{\"brand\":\"华为\",\"model\":\"荣耀X2\"}," +  
  12.             "{\"brand\":\"华为\",\"model\":\"荣耀3C\"},{\"brand\":\"华为\",\"model\":\"荣耀6 Plus\"},{\"brand\":\"华为\",\"model\":\"C2800\"},{\"brand\":\"华为\",\"model\":\"2601\"},{\"brand\":\"华为\",\"model\":\"G610S\"}," +  
  13.             "{\"brand\":\"华为\",\"model\":\"Ascend G302D\"},{\"brand\":\"华为\",\"model\":\"Ascend G6\"},{\"brand\":\"华为\",\"model\":\"Ascend G6\"},{\"brand\":\"华为\",\"model\":\"T8950N\"}," +  
  14.             "{\"brand\":\"华为\",\"model\":\"G610\"},{\"brand\":\"华为\",\"model\":\"C8813DQ\"},{\"brand\":\"华为\",\"model\":\"Y618\"},{\"brand\":\"华为\",\"model\":\"G630\"}," +  
  15.             "{\"brand\":\"华为\",\"model\":\"G521\"},{\"brand\":\"华为\",\"model\":\"荣耀畅玩4\"}]";  

仔细一看,其实就是一些对象数组,每个对象里面有2个元素,一个品牌,一个型号。

接下来,咱们就需要把这些属性全部设置到对象数组里去。


[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. java.lang.reflect.Type type = new TypeToken<List<DeviceModelBean>>(){}.getType();  
  2.           Gson gson = new Gson();  
  3.           listDeviceModel = gson.fromJson(jsonString,type);  
上面三行就可以将我们的json数组设置到List<DeviceModelBean>数组里去。在这里说明一下上面的用法,Gson可以通过toJson和fromJson方法分别把对象拆分成json数组和把json数组设置到对象集合中。Type是个接口,位于java.lang.reflect包下。formJson通过传入json字符串数组和type的接口对象从而设置到对象中去。

接下来,我们应该编写数据库帮助类和dao来完成数据库的操作。首先,我们先建立一个数据库帮助类DbHelper.java。

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public class DbHelper extends SQLiteOpenHelper {  
  2.     private Context context;  
  3.     private  static final String dbName = "bbzs.db";  
  4.     public DbHelper(Context context){  
  5.         super(context,dbName,null,1);  
  6.         this.context = context;  
  7.     }  
  8.   
  9.     //创建表  
  10.     @Override  
  11.     public void onCreate(SQLiteDatabase db) {  
  12.         String sql1 = "create table if not exists device_model_info(id integer primary key AUTOINCREMENT,model varchar(20),brand varchar(20))";  
  13.         db.execSQL(sql1);  
  14.     }  
  15.     //删除数据库  
  16.     public void deleteDb(){  
  17.         context.deleteDatabase(dbName);  
  18.     }  
  19.     @Override  
  20.     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {  
  21.   
  22.     }  
  23. }  

我们可以通过创建DbHelper对象来创建一个数据库的实例和创建表。

然后我们可以写我们的Dao。

dao的完整代码如下:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public class DeviceModelDao {  
  2.     public static final String TAG = "DeviceModelDao";  
  3.     Context context;  
  4.     SQLiteDatabase db;  
  5.     List<DeviceModelBean> listDeviceModel = null;  
  6.     public  DeviceModelDao(Context context){  
  7.         this.context = context;  
  8.     }  
  9.     //Gson解析数组到对象中去  
  10.     public List<DeviceModelBean>  addObjectToList(){  
  11.         String jsonString = "[{\"brand\":\"华为\",\"model\":\"c8818\"},{\"brand\":\"华为\",\"model\":\"Y635\"}," +  
  12.                 "{\"brand\":\"华为\",\"model\":\"Y635-CL00\"},{\"brand\":\"华为\",\"model\":\"P8 Lite\"},{\"brand\":\"华为\",\"model\":\"荣耀X2\"}," +  
  13.                 "{\"brand\":\"华为\",\"model\":\"荣耀Hol-T00\"},{\"brand\":\"华为\",\"model\":\"荣耀3X畅玩版\"}," +  
  14.                 "{\"brand\":\"华为\",\"model\":\"荣耀6\"},{\"brand\":\"华为\",\"model\":\"荣耀4C\"},{\"brand\":\"华为\",\"model\":\"荣耀X3升级版\"}," +  
  15.                 "{\"brand\":\"华为\",\"model\":\"C8816\"},{\"brand\":\"华为\",\"model\":\"C8816D\"},{\"brand\":\"华为\",\"model\":\"Mate 7\"},{\"brand\":\"华为\",\"model\":\"荣耀畅玩4C\"}," +  
  16.                 "{\"brand\":\"华为\",\"model\":\"荣耀7\"},{\"brand\":\"华为\",\"model\":\"荣耀畅玩4C\"},{\"brand\":\"华为\",\"model\":\"荣耀7\"},{\"brand\":\"华为\",\"model\":\"荣耀4A\"}," +  
  17.                 "{\"brand\":\"华为\",\"model\":\"P8\"},{\"brand\":\"华为\",\"model\":\"C2900\"},{\"brand\":\"华为\",\"model\":\"Y320\"}," +  
  18.                 "{\"brand\":\"华为\",\"model\":\"C8815\"},{\"brand\":\"华为\",\"model\":\"Mate\"},{\"brand\":\"华为\",\"model\":\"Y600\"}," +  
  19.                 "{\"brand\":\"华为\",\"model\":\"荣耀6 Plus\"},{\"brand\":\"华为\",\"model\":\"C8817L\"},{\"brand\":\"华为\",\"model\":\"G5000\"}," +  
  20.                 "{\"brand\":\"华为\",\"model\":\"C8817E\"},{\"brand:\":\"华为\",\"model\":\"荣耀6X\"},{\"brand\":\"华为\",\"model\":\"P8 Lite\"}," +  
  21.                 "{\"brand\":\"华为\",\"model\":\"Ascend P8\"},{\"brand\":\"华为\",\"model\":\"荣耀畅玩4X\"},{\"brand\":\"华为\",\"model\":\"G629\"},{\"brand\":\"华为\",\"model\":\"G620\"},{\"brand\":\"华为\",\"model\":\"荣耀X2\"}," +  
  22.                 "{\"brand\":\"华为\",\"model\":\"荣耀3C\"},{\"brand\":\"华为\",\"model\":\"荣耀6 Plus\"},{\"brand\":\"华为\",\"model\":\"C2800\"},{\"brand\":\"华为\",\"model\":\"2601\"},{\"brand\":\"华为\",\"model\":\"G610S\"}," +  
  23.                 "{\"brand\":\"华为\",\"model\":\"Ascend G302D\"},{\"brand\":\"华为\",\"model\":\"Ascend G6\"},{\"brand\":\"华为\",\"model\":\"Ascend G6\"},{\"brand\":\"华为\",\"model\":\"T8950N\"}," +  
  24.                 "{\"brand\":\"华为\",\"model\":\"G610\"},{\"brand\":\"华为\",\"model\":\"C8813DQ\"},{\"brand\":\"华为\",\"model\":\"Y618\"},{\"brand\":\"华为\",\"model\":\"G630\"}," +  
  25.                 "{\"brand\":\"华为\",\"model\":\"G521\"},{\"brand\":\"华为\",\"model\":\"荣耀畅玩4\"}]";  
  26.         try{  
  27.             java.lang.reflect.Type type = new TypeToken<List<DeviceModelBean>>(){}.getType();  
  28.             Gson gson = new Gson();  
  29.             listDeviceModel = gson.fromJson(jsonString,type);  
  30.             Log.e("TAG",type+"");  
  31.             for(Iterator iterator = listDeviceModel.iterator();iterator.hasNext();){  
  32.                     DeviceModelBean bean = (DeviceModelBean) iterator.next();  
  33.                     Log.e(TAG,bean.getModel()+bean.getBrand());  
  34.             }  
  35.         }catch (Exception e){  
  36.             Log.e(TAG,"错误:"+e.getMessage());  
  37.         }  
  38.         return listDeviceModel;  
  39.     }  
  40.     //插入数据到数据库  
  41.     public void insertModelToDb(List<DeviceModelBean> listDeviceModel){  
  42.         try{  
  43.   
  44.             DbHelper dbHelper = new DbHelper(context);  
  45.             db = dbHelper.getWritableDatabase();  
  46.             //开始事务  
  47.             db.beginTransaction();  
  48.             Log.e(TAG,listDeviceModel.size()+"");  
  49.             String sql1 = "insert into device_model_info(id,model,brand) values (?,?,?)";  
  50.             for(DeviceModelBean f :listDeviceModel){  
  51.                 db.execSQL(sql1,new Object[]{null,f.model,f.brand});  
  52.             }  
  53.         }catch (Exception e){  
  54.             e.printStackTrace();  
  55.         }finally {  
  56.             //提交  
  57.             db.setTransactionSuccessful();  
  58.             db.endTransaction();  
  59.             db.close();  
  60.   
  61.         }  
  62.   
  63.     }  
  64. }  

创建数据库的脚本为:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. create table if not exists device_model_info(id integer primary key AUTOINCREMENT,model varchar(20),brand varchar(20))  

执行插入的脚本为:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. insert into device_model_info(id,model,brand) values (?,?,?)  

这里注意的是第一个为主键id,这个必须得加上integer primary key 才会自动生成。另外,主键必须为null才可以,否则一直


在代码中的用法为:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. //创建数据库和表  
  2.         DbHelper dbHelper = new DbHelper(MyApplication.getMyApplication());  
  3.         //插入数据到数据库  
  4.         DeviceModelDao dao = new DeviceModelDao(MyApplication.getMyApplication());  
  5.         dao.insertModelToDb(dao.addObjectToList());  




  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值