Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

org.json.JSONArray cannot be converted to JSONObject

Tags:

java

json

android

I am new to JSON and I am getting the follwoing Exception:

org.json.JSONArray cannot be converted to JSONObject in the first line of try section itself.

Please help me to remove this. Here's my code:

try {   
    JSONObject json = new JSONObject(strResponse);

    //Get the element that holds the internship ( JSONArray )
    JSONArray name = json.names();
    JSONArray  internships = json.toJSONArray(name);

    //Loop the Array
    for(int i=0;i < internships.length();i++) {     
        Log.e("Message","loop");
        HashMap<String, String> map = new HashMap<String, String>();
        JSONObject e = internships.getJSONObject(i);
        map.put("id",  String.valueOf("id"));
        map.put("title", "Title :" + e.getString("title"));
        map.put("company", "Company : " +  e.getString("company"));
        map.put("category", "Category : " +  e.getString("category"));
        mylist.add(map);
    } 
} catch(JSONException e) {
    Log.e("log_tag", "Error parsing data "+e.toString());
}

this is the json I am getting from my php file

[
 {
    "id": "31",
    "title": "Business Development - Executive",
    "company": "Indidelights",
    "category": "Sales and Business Development"
 },
 {
    "id": "40",
    "title": "Business Development - Ecommerce MH",
    "company": "Ram Gopal & Co",
    "category": "Sales and Business Development"
 },
 {
    "id": "41",
    "title": "Sales and Business development intern",
    "company": "Esanchalak",
    "category": "Sales and Business Development"
 },
 {
    "id": "42",
    "title": "Purchase Executive",
    "company": "Winni.in",
    "category": "Marketing"
 },
 {
    "id": "43",
    "title": "Marketing Intern",
    "company": "Walkover Web Solutions Pvt. Ltd.",
    "category": "Marketing"
 },
 {
    "id": "44",
    "title": "Marketing Intern",
    "company": "SkillKindle Learning Pvt Ltd",
    "category": "Marketing"
 },
 {
    "id": "45",
    "title": "Graphic Designer",
    "company": "Stylopa",
    "category": "Graphic Design / Art Work"
 },
 {
    "id": "46",
    "title": "Graphic Designer",
    "company": "LycondonFX",
    "category": "Graphic Design / Art Work"
 },
 {
    "id": "47",
    "title": "Web Designer",
    "company": "Xapify LLC",
    "category": "Software"
 },
 {
    "id": "48",
    "title": "Web Designer (Frontend)",
    "company": "gotrademark.in",
    "category": "Web Design and Development"
 },
 {
    "id": "49",
    "title": "Content Writing Intern",
    "company": "National Entrepreneurship Network",
    "category": "Content Writing / Journalism"
 },
 {
    "id": "50",
    "title": "Content Writing Intern",
    "company": "Pragmatum Training Pvt Ltd",
    "category": "Content Writing / Journalism"
 },
 {
    "id": "51",
    "title": "HR Intern",
    "company": "GATI Kintetsu Express Pvt Ltd",
    "category": "HR / Recruitment"
 },
 {
    "id": "52",
    "title": "Pharma Intern",
    "company": "Qlinics Health Care Pvt Ltd",
    "category": "BioTechnology / Pharma"
 },
 {
    "id": "53",
    "title": "Android Developer",
    "company": "InoXapps Mobile Solutions Pvt Ltd",
    "category": "Mobile App Development"
 },
 {
    "id": "54",
    "title": "Mobile App developer",
    "company": "RV Media Inc",
    "category": "Mobile App Development"
 },
 {
    "id": "55",
    "title": "Electronics Intern",
    "company": "GA SOFTWARE TECHNOLOGIES PVT LTD",
    "category": "Electronics Engineering"
 }
 ]
like image 861
DPK27 Avatar asked Jul 03 '13 06:07

DPK27


People also ask

Can we convert JSONArray to JSONObject?

We 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

This

JSONObject json = new JSONObject(strResponse);
// your strResponse is a json array 

should be

JSONArray jsonarray = new JSONArray(strResponse);

[ represents json array node

{ represents json object node

for(int i=0; i < jsonarray.length(); i++) {
    JSONObject jsonobject = jsonarray.getJSONObject(i);
    String id       = jsonobject.getString("id");
    String title    = jsonobject.getString("title");
    String company  = jsonobject.getString("company");
    String category = jsonobject.getString("category");
}
like image 177
Raghunandan Avatar answered Sep 27 '22 18:09

Raghunandan


You should probably initialize json as a JSONArray:

JSONObject json = new JSONObject(strResponse);

Should then be:

JSONArray json = new JSONArray(strResponse);

However, that wouldn't work with the following two operations:

JSONArray name = json.names(); //.names() doesn't exist in JSONArray
JSONArray  internships = json.toJSONArray(name); // Is instead to be seen as

That would be alright if you just alter your loop to get the JSONObject from json instead (thus removing the dependency towards .names():

JSONObject e = json.getJSONObject(i);

Edit: Full code

try {   
    JSONArray internships = new JSONArray(strResponse);

    //Loop the Array
    for(int i=0;i < internships.length();i++) {     
        Log.e("Message","loop");
        HashMap<String, String> map = new HashMap<String, String>();
        JSONObject e = internships.getJSONObject(i);
        map.put("id",  String.valueOf("id"));
        map.put("title", "Title :" + e.getString("title"));
        map.put("company", "Company : " +  e.getString("company"));
        map.put("category", "Category : " +  e.getString("category"));
        mylist.add(map);
    } 
} catch(JSONException e) {
    Log.e("log_tag", "Error parsing data "+e.toString());
}
like image 31
karllindmark Avatar answered Sep 27 '22 18:09

karllindmark