如何从单个json对象解析多个数组并将它们存储在android的Sqlite数据库中 实际JSOn数据
{
"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
}
],
这是我的代码,我遇到了问题
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,
}
这是我的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)
答案 0 :(得分:1)
以下代码为您提供结果
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();
}
}