2012-09-24 55 views
3

在拿JSON數據我得到錯誤:JSONArray不能被轉換爲JSONObject的錯誤

JSONArray cannot be converted to JSONObject

代碼JSON生成:

JSONObject parent = new JSONObject(); 
DatabaseHandler dbh = new DatabaseHandler(getApplicationContext()); 
      for(int i=0; i < allEds.size(); i++){ 
       String edsText = allEds.get(i).getText().toString();           
       //spinner = allSpns.get(i); 
       String spinSelected=allSpns.get(i).getSelectedItem().toString();     
       try 
       { 
        JSONObject json = new JSONObject();   
        json.put("Id", i); 
        json.put("FieldName", edsText); 
        json.put("FieldType",spinSelected); 
        parent.accumulate("data", json); 



       } 
       catch (JSONException e) 
       { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
       }     

      } 
Generated json is 
      {"data": 
[{"FieldType":"Account Number","FieldName":"r","Id":0}, 
    {"FieldType":"Net  Banking Id","FieldName":"tt","Id":1} 
]} 
code for json read 
------------------ 
JSONObject jsonObj = new JSONObject(folderStructure); 
     JSONObject data = jsonObj.getJSONObject("data"); 
     String id = data.getString("Id"); 
     String value = data.getString("FieldName"); 
     Log.d("Item name: ", value);  

在閱讀上面的JSON會收到錯誤 任何一點毛病用代碼?

+0

添加代碼。 – jeet

+0

@jeet增加了你的 – user1682133

+0

你放置你的json代碼...在JavaScript中? –

回答

11

變化

JSONObject data = jsonObj.getJSONObject("data"); 

JSONArray data = jsonObj.getJSONArray("data"); 

作爲數據的值是不JsonArray的JSONObject。

,並獲得各自的ID和字段名稱,你應該遍歷這個JSONArray,具體如下:

for(int i=0; i<data.length(); i++) 
{ 
    JSONObject obj=data.getJSONObject(i); 
    String id = obj.getString("Id"); 
     String value = obj.getString("FieldName"); 
     Log.d("Item name: ", value); 
} 
+0

thnks朋友:)\ – user1682133

+0

@ user1682133歡迎您,請檢查我編輯的答案,如果它完全回答您的問題,或不。 – jeet

+0

我想改變JSONObject obj = data.JSONObject(i); to JSONObject obj = data.getJSONObject(i);和它的工作,我thnks :) – user1682133

0

data是一個數組,而不是一個對象:

JSONArray data = jsonObj.getJSONArray("data"); 
+0

確定它現在確定那麼你能告訴我如何從這個數據? – user1682133

+0

我目前的代碼是String value = data.getString(「FieldName」); – user1682133

1

使用此方法:閱讀JSON太

private void showJSON(String response) { 
     list = new ArrayList<>(); 
     String name = null; 
     try { 

      JSONArray jsonObject = new JSONArray(response); 
      for(int i = 0; i < jsonObject.length(); i++) { 
       JSONObject obj = jsonObject.getJSONObject(i); 
       //store your variable 
       list.add(obj.getString("Name")); 
      } 
//   JSONArray result = jsonObject.getJSONArray(""); 
//   JSONObject collegeData = result.getJSONObject(0); 
//   list.add(jsonObject.getString(collegeData.getString("Name"))); 
      Toast.makeText(getActivity(), name, Toast.LENGTH_LONG).show(); 
      city_list.addAll(list); 
      adapter.notifyDataSetChanged(); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
      // Toast.makeText(getApplicationContext(), response, Toast.LENGTH_LONG).show(); 
     } 

    } 
相關問題