yash_5111
yash_5111

Reputation: 45

how to parse multiple arrays from single json object and store them in Sqlite database in android

how to parse multiple arrays from single json object and store them in Sqlite database in android
Actual JSOn data

{
  "cityMasterEntity": [//// JSon array 1
    {
      "CityId": 1,
      "CityName": "Ahmedabad",
      "CreatedDate": "\/Date(1373091319697+0530)\/",
      "IsActive": true,
      "StateId": 6,
      "StateName": "Gujarat\u000d\u000a",
      "UpdatedDate": null
    },
    {
      "CityId": 3,
      "CityName": "Rajkot",
      "CreatedDate": "\/Date(1373091319697+0530)\/",
      "IsActive": true,
      "StateId": 6,
      "StateName": "Gujarat\u000d\u000a",
      "UpdatedDate": null
    },
    {
      "CityId": 2,
      "CityName": "Surat",
      "CreatedDate": "\/Date(1373091319697+0530)\/",
      "IsActive": true,
      "StateId": 6,
      "StateName": "Gujarat\u000d\u000a",
      "UpdatedDate": null
    }
  ],
  "countryMasterEntity": [ ////Json Array 2 
    {
      "CountryId": 1,             ///>>  i am not able parse and store this array in database 
      "CountryName": "India",
      "CreatedDate": "\/Date(1373091319697+0530)\/",
      "IsActive": true,
      "UpdatedDate": null
    }
  ],

This my code where i m expriencing problem

protected void onPostExecute(JSONObject result) { 
    progressdialog.dismiss(); 
    System.out.println("JSONObject is"+result); 
    try { 
         citymaster = result.getJSONArray("cityMasterEntity"); 
         for(int i=0;i<citymaster.length();i++) { 
              result =citymaster.getJSONObject(i); 
              valuesCity.put("_id", i + 1); 
              int cityid1 = result.getInt("CityId"); 
              valuesCity.put("cityid", cityid1); 
              String cityname1 = result.getString("CityName"); 
              valuesCity.put("cityname", cityname1); 
              String createddate1 = result.getString("CreatedDate");
              valuesCity.put("createddate", createddate1); 
              String isactive1 = result.getString("IsActive"); 
              valuesCity.put("isactive", isactive1); 
              int stateid1 = result.getInt("StateId"); 
              valuesCity.put("stateid", stateid1); 
              String statename1 = result.getString("StateName"); 
              valuesCity.put("statename", statename1); 
              String updateddate1 = result.getString("UpdatedDate");
              valuesCity.put("updateddate", updateddate1);
              newrummydb.insert(Database.DATABASE_TABLE, null, valuesCity); 
        }

    countrymaster=result.getJSONArray("countryMasterEntity");/// >>>problem is in this line  
        for (int j=0;j<countrymaster.length();j++)
        {
             result =countrymaster.getJSONObject(j);// This line also
             valuesCountry.put("_id", j + 1);

             int countryid2 = result.getInt("CountryId");
                 valuesCountry.put(countryid, countryid2);
             String countryname2 = result.getString("CountryName");
             valuesCountry.put(countryname, countryname2);
             String createddate2 = result.getString("CreatedDate");
             valuesCountry.put(Ccreateddate, createddate2);
             String isactive2 = result.getString("IsActive");
             valuesCountry.put(Cisactive, isactive2);
             String updateddate2 = result.getString("UpdatedDate");
             valuesCountry.put(Cupdateddate, updateddate2);
             newrummydb.insert(Database.DATABASE_TABLE_COUNTRY, null, 
        }

This my logcat

     04-17 13:13:59.922: E/SQLiteDatabase(30756): Error inserting stateid=6 isactive=true _id=1 statename=Gujarat
        04-17 13:13:59.922: E/SQLiteDatabase(30756):  updateddate=null createddate=/Date(1373091319697+0530)/ cityname=Ahmedabad cityid=1
        04-17 13:13:59.922: E/SQLiteDatabase(30756): android.database.sqlite.SQLiteConstraintException: column _id is not unique (code 19)
        04-17 13:13:59.922: E/SQLiteDatabase(30756):    at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
        04-17 13:13:59.922: E/SQLiteDatabase(30756):    at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:775)
        04-17 13:13:59.922: E/SQLiteDatabase(30756):    at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
        04-17 13:13:59.922: E/SQLiteDatabase(30756):    at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
        04-17 13:13:59.922: E/SQLiteDatabase(30756):    at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
        04-17 13:13:59.922: E/SQLiteDatabase(30756):    at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
        04-17 13:13:59.922: E/SQLiteDatabase(30756):    at 

     com.example.newrummyjson.NewRummyActivity$JSONParsing.onPostExecute(NewRummyActivity.java:201)
        04-17 14:38:41.934: E/SQLiteDatabase(21980):    at android.os.AsyncTask.finish(AsyncTask.java:631)
        04-17 14:38:41.934: E/SQLiteDatabase(21980):    at android.os.AsyncTask.access$600(AsyncTask.java:177)
        04-17 14:38:41.934: E/SQLiteDatabase(21980):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
        04-17 14:38:41.934: E/SQLiteDatabase(21980):    at android.os.Handler.dispatchMessage(Handler.java:99)
        04-17 14:38:41.934: E/SQLiteDatabase(21980):    at android.os.Looper.loop(Looper.java:137)
        04-17 14:38:41.934: E/SQLiteDatabase(21980):    at android.app.ActivityThread.main(ActivityThread.java:4745)
        04-17 14:38:41.934: E/SQLiteDatabase(21980):    at java.lang.reflect.Method.invokeNative(Native Method)
        04-17 14:38:41.934: E/SQLiteDatabase(21980):    at java.lang.reflect.Method.invoke(Method.java:511)
        04-17 14:38:41.934: E/SQLiteDatabase(21980):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
        04-17 14:38:41.934: E/SQLiteDatabase(21980):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        04-17 14:38:41.934: E/SQLiteDatabase(21980):    at dalvik.system.NativeStart.main(Native Method)

Upvotes: 0

Views: 1489

Answers (1)

Vijay Vala
Vijay Vala

Reputation: 73

Following code get you result

JSONParser jParser = new JSONParser();
        JSONObject json = jParser.getJSONFromUrl(json_url);
            // TODO Auto-generated method stub
            JSONArray countryMasterEntity= json.getJSONArray("countryMasterEntity");

            if(countryMasterEntity!=null)
            {
            // looping through All Contacts
            for(int i = 0; i < countryMasterEntity.length(); i++){
                JSONObject c = countryMasterEntity.getJSONObject(i);
                Cursor cr = db.rawQuery("select * from `country` where `id`='"+c.getString("id")+"'",null);

                    String query=
                "Insert into country(id,countryname,createdate,isactive)"+
                "values("+
                c.getString("id")+",'"+
                c.getString("countryname")+"','"+
                c.getString("createdate")+"','"+
                c.getString("isactive")
                +"')";
                    db.execSQL(query);
                Log.d("Insert", query);

                cr.close();
            }
            }

Upvotes: 1

Related Questions