2017-03-18 63 views
0

我只是試圖讓存儲在我的JSON文件中的值,並將其保存到SQLite數據庫:解析嵌套的JSON對象,並存儲在數據庫中的Android

這是我的JSON文件:

{ 
    "list": { 
    "meta": { 
     "count": 132, 
     "start": 0, 
     "type": "resource-list" 
    }, 
    "resources": [ 
     { 
     "resource": { 
      "classname": "Quote", 
      "fields": { 
      "date": "2017-03-16", 
      "price": 3.6720000000000002, 
      "type": "currency", 
      "symbol": "AED=X" 
      } 
     } 
     }, 
     { 
     "resource": { 
      "classname": "Quote", 
      "fields": { 
      "date": "2017-03-16", 
      "price": 65.075000000000003, 
      "type": "currency", 
      "symbol": "AFN=X" 
      } 
     } 
     }, 
     { 
............. 
} 
............ 

我已經試過這樣的,但得到的異常:

JSONObject mainObj = null; 
try { 
    mainObj = new JSONObject(JSON); 
    JSONObject getSth = mainObj.getJSONObject("list"); 
    if(mainObj != null){ 
     JSONArray list = getSth.getJSONArray("resources"); 
     if(list != null){ 
      for(int i = 0; i < list.length();i++){ 
       JSONObject elem = list.getJSONObject(i); 
       if(elem != null){ 
        JSONObject prods = elem.getJSONObject("fields"); 
        Object level = prods.get("type"); 
        Toast.makeText(getApplicationContext(),""+level.toString(),Toast.LENGTH_LONG).show(); 
       } 
      } 
     } 
    } 
}catch (Exception e){ 
    Toast.makeText(getApplicationContext(),""+e.toString(),Toast.LENGTH_LONG).show(); 
} 

我得到異常:在沒有字段值...

並請提供一些建議,將這些值存儲在(行字段)名稱,獎品,符號和類型的數據庫表(matrotable)中,我可以嘗試通過製作String Array並檢索和存儲sqlite的值,是否有其他易選項... 感謝

回答

1

fields對象是內部resource對象,這樣做

for(int i = 0; i < list.length();i++){ 
       JSONObject elem = list.getJSONObject(i); 
       if(elem != null){ 
        JSONObject prods = elem.getJSONObject("resource") 
              .getJSONObject("fields"); 

        Object level = prods.get("type"); 
        Toast.makeText(getApplicationContext(),""+level.toString(),Toast.LENGTH_LONG).show(); 
       } 
      } 
"resources": [      // resources list 
    {         // object i 
    "resource": {     // fields are inside "resource" object 
     "classname": "Quote", 
     "fields": { 
     "date": "2017-03-16", 
     "price": 3.6720000000000002, 
     "type": "currency", 
     "symbol": "AED=X" 
     } 
    } 
    } 
+0

是日Thnx,我需要存儲SQLite和後來檢索,我怎麼能輕鬆實現這一點,我的目的是通過將這些字段添加到字符串數組並將其復原?任何其他建議。 –

+1

@Learner然後你應該看一下'GSON',然後你就可以創建一個pojo列表,試試[GSON docs link](http://stackoverflow.com/documentation/android/4158/gson#t= 201703181903413153608) –

1

你缺少resource JOSNObject解析...

for(int i = 0; i < list.length();i++){ 
       JSONObject elem = list.getJSONObject(i); 
       JSONObject resource = elem.getJSONObject("resource"); 
       if(resource != null){ 
        JSONObject prods = resource.getJSONObject("fields"); 
        Object level = prods.get("type"); 
        Toast.makeText(getApplicationContext(),""+level.toString(),Toast.LENGTH_LONG).show(); 
       } 
      } 
+0

是的thnx,我需要存儲到sqlite,然後檢索後,我怎麼能實現這一點,我的去是通過添加這些字段到字符串數組和retieveing它?任何其他建議。 –

+1

看看這個:[答案](http://stackoverflow.com/questions/6288500/save-data-from-json-object-to-database-sqlite)與getter和setter創建一個模型類...或者使用GSON和Pojo的其他選項。 – rafsanahmad007

0

我建議您用最簡單和最簡單的方式來解析JSON響應,以避免這樣的問題:

1-通過使用這個工具生成模型類:http://www.jsonschema2pojo.org/下載並將生成的類添加到您的模型包中。

2 - 添加這個依賴於你的gradle這個文件:

compile 'com.google.code.gson:gson:2.4' 

3-調用此方法來解析你的迴應:

Gson gson = new Gson(); 
ResponseModel responseModel = gson.fromJson(json, ResponseModel.class);