Reputation: 2257
I'm trying to develop a json parser application. Here I'm getting a json parser exception error like "jsonarray cannot be converted to jsonobject"
I have following the below JSON format :
[
{
"status": "ok",
"data": [
{
"term_id": "28",
"name": "All Meets",
},
{
"term_id": "4",
"name": "Classic Cars",
},
{
"term_id": "30",
"name": "Trucks",
}
],
"controller": "user1"
},
0
]
This is the code where the exception is thrown:
class GetCategories extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... args) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
JSONObject json = jsonParser.makeHttpRequest(url_getcategories, "POST", params);
Log.d("Get Categories", json.toString());
try {
success = json.getString(TAG_SUCCESS);
if (success.equalsIgnoreCase("ok")) {
JSONArray sys = json.getJSONArray(TAG_CATEGORY);
System.out.println("List of Array =" + " " + sys);
for (int i = 0; i < sys.length(); i++) {
JSONObject c = sys.getJSONObject(i);
String cartitle = c.getString("name");
System.out.println("List of Categories =" + " " + cartitle);
}
} else {
error = json.getString(TAG_ERROR);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String file_url) {
super.onPostExecute(file_url);
}
}
From the above code what's wrong in the functionality? Please check it and provide a solution.
Upvotes: 0
Views: 91
Reputation: 164
When i put your J son format in `http://www.jsoneditoronline.org/` , It gave me issue.
So, i formatted it to
{
"status": "ok",
"data": [
{
"term_id": "28",
"name": "All Meets"
},
{
"term_id": "4",
"name": "Classic Cars"
},
{
"term_id": "30",
"name": "Trucks"
}
],
"controller": "user1"
}
I think your code will work, with this format. You can refer Android JSON Parser Tutorial for more detailed solution for your issue.
Upvotes: 2
Reputation: 24848
Try this way,hope this will help you to solve your problem.
class GetCategories extends AsyncTask<String, String, ArrayList<HashMap<String,String>>> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
protected ArrayList<HashMap<String,String>> doInBackground(String... args) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
JSONArray json = jsonParser.makeHttpRequest(url_getcategories, "POST", params);
ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String, String>>();
try {
if (json.getJSONObject(0).getString(TAG_SUCCESS).equalsIgnoreCase("ok")) {
JSONArray sys = json.getJSONArray(TAG_CATEGORY);
for (int i = 0; i < sys.length(); i++) {
HashMap<String,String> row = new HashMap<String, String>();
row.put("term_id",sys.getJSONObject(i).getString("term_id"));
row.put("name", sys.getJSONObject(i).getString("name"));
list.add(row);
}
}
else{
error = json.getJSONObject(0).getString(TAG_ERROR);
}
} catch (JSONException e) {
e.printStackTrace();
}
return list;
}
protected void onPostExecute(ArrayList<HashMap<String,String>> result) {
super.onPostExecute(result);
}
}
Upvotes: 0
Reputation: 2626
Your's is a JSON array []
and not a JSON object {}
. Convert the string to a JSON Array first.
Upvotes: 0
Reputation: 20520
There's a difference between a JSON array and a JSON object. The former is contained in []
and the latter in {}
.
You've got an array, and you're trying to create an object from it.
If the world were a sensible place, JSONObject
and JSONArray
would both descend from the same class, and you'd be able to instantiate and then ask it whether it's an array or an object. But sadly the world does silly things sometimes.
Upvotes: 0