arinze arinze - 1 year ago 68
JSON Question

Display Json data in android

i am trying to display json data in an android application but i have having difficulty i think is may have to do with the way the json file was formated .i want to get the value of name and code in the headers array. and it is not working

this is the json file

status: "SUCCESS",
id: "4",
name: "GLO",
code: "GLO",
background_color_code: "15B709",
text_color_code: "ffffff",
statusMessage: "Hi +234805, an ACCESS FEE of N20.00 will be charged in order to access this Platform"
statusMessage: "Movies Loaded successfully",
caption: "Discover"

this is the javaclass

package com.example.cann;

import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONStringer;

import android.os.AsyncTask;
import android.os.Bundle;

import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class CategoryActivity extends ListActivity {

private static final String TAG_POSTS = "headers";
public static final String TAG_PIC = "name";
public static final String TAG_NOTE = "code";
JSONParser jParser;
private static final String URL_CATEGORY = "";
private BaseAdapter mAdapter;
private ListView lv;

protected void onCreate(Bundle savedInstanceState) {
lv = getListView();

lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {

Toast.makeText(CategoryActivity.this, "Item selected: " + position,

// Uncomment this to start a new Activity for a chosen item
/* Intent i = new Intent(getApplicationContext(),

String category_id = ((TextView) view
i.putExtra("category_id", category_id);


new LoadComments().execute();

class LoadComments extends AsyncTask<Void, Void, ArrayList<HashMap<String,String>>> {
private ProgressDialog pDialog;
protected void onPreExecute() {
pDialog = new ProgressDialog(CategoryActivity.this);



protected ArrayList<HashMap<String, String>> doInBackground(Void... arg0) {

ArrayList<HashMap<String, String>> categoryList = new ArrayList<HashMap<String, String>>();
jParser = new JSONParser();

JSONObject json = jParser.getJSONFromUrl(URL_CATEGORY);

try {
JSONArray categories =json.getJSONArray("headers");
for (int i = 0; i < categories.length(); i++) {
String state = categories.getJSONObject(i).getString("name");
String status = categories.getJSONObject(i).getString("code");

HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_PIC, state);
map.put(TAG_NOTE, status);

}catch (Throwable e){
return categoryList;

protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
mAdapter = new CategoryListAdapter(CategoryActivity.this,result);

i am getting this error

07-24 12:20:50.849 16574-16591/com.example.cann W/System.err? org.json.JSONException: Value at headers of type java.lang.String cannot be converted to JSONArray
07-24 12:20:50.852 16574-16591/com.example.cann W/System.err? at org.json.JSON.typeMismatch(
07-24 12:20:50.852 16574-16591/com.example.cann W/System.err? at org.json.JSONObject.getJSONArray(
07-24 12:20:50.853 16574-16591/com.example.cann W/System.err? at com.example.cann.CategoryActivity$LoadComments.doInBackground(
07-24 12:20:50.853 16574-16591/com.example.cann W/System.err? at com.example.cann.CategoryActivity$LoadComments.doInBackground(

Answer Source

The JSON file you provided is not in a valid JSON format. And also 'headers' attribute contains a JSONObject, not JSONArray. And you got JSON Exception when you tries to get JSONObject as JSONArray.

You may try this:

   "status": "SUCCESS",
   "headers": [
      "id": "4",
      "name": "GLO",
      "code": "GLO",
      "background_color_code": "15B709",
      "text_color_code": "ffffff",
      "statusMessage": "Hi +234805, an ACCESS FEE of N20.00 will be charged in order to access this Platform"
      "id": "5",
      "name": "PLU",
      "code": "PLU",
      "background_color_code": "15B709",
      "text_color_code": "ffffff",
      "statusMessage": "Hi +234805, an ACCESS FEE of N20.00 will be charged in order to access this Platform"
   "statusMessage": "Movies Loaded successfully",
   "caption": "Discover"

Please first check whether your JSON data is valid or not. You may take help from here or here