JSONArray无法转换为JSONObject错误

use*_*133 4 parsing android json

在获取json数据时,我收到错误:

JSONArray无法转换为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);    
Run Code Online (Sandbox Code Playgroud)

在阅读上面的json时遇到错误代码有什么问题吗?

jee*_*eet 13

更改

JSONObject data = jsonObj.getJSONObject("data"); 
Run Code Online (Sandbox Code Playgroud)

JSONArray data = jsonObj.getJSONArray("data");
Run Code Online (Sandbox Code Playgroud)

由于数据的值是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);
}
Run Code Online (Sandbox Code Playgroud)