Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSONArray cannot be converted to JSONObject error

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??

like image 351
user1682133 Avatar asked Sep 24 '12 06:09

user1682133


People also ask

Can we convert JSONArray to JSONObject?

Core Java bootcamp program with Hands on practiceWe can also add a JSONArray to JSONObject. We need to add a few items to an ArrayList first and pass this list to the put() method of JSONArray class and finally add this array to JSONObject using the put() method.

Is JSONArray a JSONObject?

JSONObject and JSONArray are the two common classes usually available in most of the JSON processing libraries. A JSONObject stores unordered key-value pairs, much like a Java Map implementation. A JSONArray, on the other hand, is an ordered sequence of values much like a List or a Vector in Java.


2 Answers

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);
}
like image 157
jeet Avatar answered Oct 02 '22 16:10

jeet


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

    }
like image 31
Tara Avatar answered Oct 02 '22 15:10

Tara