天天看點

GSON解析JSON儲存到資料庫

今天給大家帶來的文章為通過Gson解析json資料并快速儲存至資料庫的文章。我們要儲存是json對象數組,本文中的json數組并非從背景擷取,為了示範,直接手動構造。

需要儲存到資料庫的是手機的品牌和型号。是以,我們需要建立一個bean實體類去儲存我們的型号和品牌。在這,我先介紹2個工具,一個是Google官方的Gson解析jar包。

名為Gson.jar,這個百度下載下傳就可以了。另外一個是序列化插件Parcelable。在setting---->>>>plugin----->搜尋Parcelable。

GSON解析JSON儲存到資料庫

好了,下面建立我們的bean,取名DeviceModelBean.Java。添加屬性後,右鍵選擇Generate--->>>Parcelable,然後快速直接序列化,對bean我們最好養成習慣,先序列化。

[java]  view plain  copy  

GSON解析JSON儲存到資料庫
GSON解析JSON儲存到資料庫
  1. package com.mero.wyt_register.bean;  
  2. import android.os.Parcel;  
  3. import android.os.Parcelable;  
  4. public class DeviceModelBean implements Parcelable {  
  5.     public String getBrand() {  
  6.         return brand;  
  7.     }  
  8.     public void setBrand(String brand) {  
  9.         this.brand = brand;  
  10.     }  
  11.     public String getModel() {  
  12.         return model;  
  13.     }  
  14.     public void setModel(String model) {  
  15.         this.model = model;  
  16.     }  
  17.     public String model;//型号  
  18.     public String brand;//品牌  
  19.     @Override  
  20.     public int describeContents() {  
  21.         return 0;  
  22.     }  
  23.     @Override  
  24.     public void writeToParcel(Parcel dest, int flags) {  
  25.         dest.writeString(this.model);  
  26.         dest.writeString(this.brand);  
  27.     }  
  28.     protected DeviceModelBean(Parcel in) {  
  29.         this.model = in.readString();  
  30.         this.brand = in.readString();  
  31.     }  
  32.     public static final Parcelable.Creator<DeviceModelBean> CREATOR = new Parcelable.Creator<DeviceModelBean>() {  
  33.         @Override  
  34.         public DeviceModelBean createFromParcel(Parcel source) {  
  35.             return new DeviceModelBean(source);  
  36.         }  
  37.         @Override  
  38.         public DeviceModelBean[] newArray(int size) {  
  39.             return new DeviceModelBean[size];  
  40.         }  
  41.     };  
  42. }  

接下來,再看看我們的json字元串的内容。這個字元串是我按照預期目的而構造的。

json字元串内容如下:

[java]  view plain  copy  

GSON解析JSON儲存到資料庫
GSON解析JSON儲存到資料庫
  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  

GSON解析JSON儲存到資料庫
GSON解析JSON儲存到資料庫
  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  

GSON解析JSON儲存到資料庫
GSON解析JSON儲存到資料庫
  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.     @Override  
  10.     public void onCreate(SQLiteDatabase db) {  
  11.         String sql1 = "create table if not exists device_model_info(id integer primary key AUTOINCREMENT,model varchar(20),brand varchar(20))";  
  12.         db.execSQL(sql1);  
  13.     }  
  14.     //删除資料庫  
  15.     public void deleteDb(){  
  16.         context.deleteDatabase(dbName);  
  17.     }  
  18.     @Override  
  19.     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {  
  20.     }  
  21. }  

我們可以通過建立DbHelper對象來建立一個資料庫的執行個體和建立表。

然後我們可以寫我們的Dao。

dao的完整代碼如下:

[java]  view plain  copy  

GSON解析JSON儲存到資料庫
GSON解析JSON儲存到資料庫
  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.             DbHelper dbHelper = new DbHelper(context);  
  44.             db = dbHelper.getWritableDatabase();  
  45.             //開始事務  
  46.             db.beginTransaction();  
  47.             Log.e(TAG,listDeviceModel.size()+"");  
  48.             String sql1 = "insert into device_model_info(id,model,brand) values (?,?,?)";  
  49.             for(DeviceModelBean f :listDeviceModel){  
  50.                 db.execSQL(sql1,new Object[]{null,f.model,f.brand});  
  51.             }  
  52.         }catch (Exception e){  
  53.             e.printStackTrace();  
  54.         }finally {  
  55.             //送出  
  56.             db.setTransactionSuccessful();  
  57.             db.endTransaction();  
  58.             db.close();  
  59.         }  
  60.     }  
  61. }  

建立資料庫的腳本為:

[java]  view plain  copy  

GSON解析JSON儲存到資料庫
GSON解析JSON儲存到資料庫
  1. create table if not exists device_model_info(id integer primary key AUTOINCREMENT,model varchar(20),brand varchar(20))  

執行插入的腳本為:

[java]  view plain  copy  

GSON解析JSON儲存到資料庫
GSON解析JSON儲存到資料庫
  1. insert into device_model_info(id,model,brand) values (?,?,?)  

這裡注意的是第一個為主鍵id,這個必須得加上integer primary key 才會自動生成。另外,主鍵必須為null才可以,否則一直

在代碼中的用法為:

[java]  view plain  copy  

GSON解析JSON儲存到資料庫
GSON解析JSON儲存到資料庫
  1. //建立資料庫和表  
  2.         DbHelper dbHelper = new DbHelper(MyApplication.getMyApplication());  
  3.         //插入資料到資料庫  
  4.         DeviceModelDao dao = new DeviceModelDao(MyApplication.getMyApplication());  
  5.         dao.insertModelToDb(dao.addObjectToList());  
GSON解析JSON儲存到資料庫