6

While fetching json data I am getting error:

JSONArray cannot be converted to JSONObject

Code for json generate:

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);    

While reading the above json am getting errors Any thing wrong with the code??

2
  • add code for reading json too. Commented Sep 24, 2012 at 6:02
  • where you placed your json code... in javascript? Commented Sep 24, 2012 at 6:07

3 Answers 3

15

Change

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

to

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

As value of data is JsonArray not JSONObject.

And to Get individual Ids and Field Names, you should loop through this JSONArray, as follows:

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);
}
Sign up to request clarification or add additional context in comments.

3 Comments

@user1682133 you are welcome, please check my edited answer, if it answers your question fully, or not.
i think change JSONObject obj=data.JSONObject(i); to JSONObject obj=data.getJSONObject(i); and its working for me , thnks :)
@jeet i have same problem .........when i put this JSONObject result=jsonobject.getJSONObject("result"); JSONArray result=jsonobject.getJSONArray("result"); then i got to create result array rename ...it mean create array diffrent name ..........what i do in this case ...........if any idea .......thanks
3

Use this method:

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();
        }

    }

Comments

0

data is an array, not an object:

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

1 Comment

my current code is String value = data.getString("FieldName");

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.