Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
i am trying to show my json string in android listView but Found an Error
Note: i am Using WCF services For converting my data to json format.

Error parsing data org.json.JSONException: Value [{"Id":1,"Name":"Umair","SSN":"11111111"},{"Id":2,"Name":"khan","SSN":"2222222"}] at JSONDataResult of type java.lang.String cannot be converted to JSONArray

My Json String ...
{"JSONDataResult":"[{\"Id\":1,\"Name\":\"Umair\",\"SSN\":\"11111111\"},{\"Id\":2,\"Name\":\"khan\",\"SSN\":\"2222222\"}]"}

My Android Coding :..
protected void showList(){


try {

// myJSON = myJSON.replace("\"","'");
JSONObject jsonObj = new JSONObject(myJSON);

JArray = jsonObj.getJSONArray(TAG_Obj);


for(int i=0;i<jarray mode="hold"> JSONObject c = JArray.getJSONObject(i);


id = c.getString(TAG_ID);
name = c.getString(TAG_Name);
ssn = c.getString(TAG_SSN);


HashMap<string,string> Hashmappersons = new HashMap<string,string>();

Hashmappersons.put(TAG_ID,id);
Hashmappersons.put(TAG_Name,name);
Hashmappersons.put(TAG_SSN,ssn);

ArraypersonList.add(Hashmappersons);

}


ListAdapter listadapter = new SimpleAdapter(

MainActivity.this, ArraypersonList,
R.layout.list_item,
new String[]{TAG_ID,TAG_Name,TAG_SSN},
new int[]{R.id.tv_id, R.id.tv_Name,R.id.tv_SSN}

);

listv.setAdapter(listadapter);

}
catch (JSONException e) {
e.printStackTrace();

Log.v("" + e, "Error" + e);
Log.e("log_tag", "Error parsing data "+e.toString());
Log.e("log_tag", "Failed data was:\n" +listv);

// tv_Er.setText("Error: " + e);

Toast.makeText(getApplicationContext(),""+e+"",Toast.LENGTH_SHORT).show();
}

}


public void getData(){
class GetDataJSON extends AsyncTask<string,> {

@Override
protected String doInBackground(String... params) {

DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());

// HttpPost httppost = new HttpPost(url);

// Depends on your web service
// httppost.setHeader("Accept", "application/json");
// httppost.setHeader("Content-type", "application/json");

HttpGet httpget = new HttpGet(url);
httpget.setHeader("Accept", "application/json");
httpget.setHeader("Content-type", "application/json");

// DefaultHttpClient httpClient = new DefaultHttpClient();


InputStream inputStream = null;
String result = null;
try {
// HttpResponse response = httpclient.execute(httppost);
HttpResponse response = httpclient.execute(httpget);

HttpEntity entity = response.getEntity();
inputStream = entity.getContent();

// HttpResponse response = httpClient.execute(request);
// HttpEntity responseEntity = response.getEntity();
// inputStream = responseEntity.getContent();

// json is UTF-8 by default

BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);

// BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, HTTP.UTF_8), 8);

StringBuilder sb = new StringBuilder();

String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
result = sb.toString();
} catch (Exception e) {
// Oops
}
finally {
try{
if(inputStream != null)inputStream.close();
}
catch(Exception squish)
{

}
}
return result;
}

@Override
protected void onPostExecute(String result){
myJSON=result;
showList();
}
}
GetDataJSON g = new GetDataJSON();
g.execute();
}

What I have tried:

i am trying to show my json string in android listView but always Found an Error
value of type java.lang.String cannot be converted to JSONArray .
Posted
Updated 26-Nov-17 17:53pm

1 solution

In your Json String, Remove the double quotes and the string would be as follows

{"JSONDataResult":[{\"Id\":1,\"Name\":\"Umair\",\"SSN\":\"11111111\"},{\"Id\":2,\"Name\":\"khan\",\"SSN\":\"2222222\"}]}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900