解析数据时出错org.json.jsonexception:value .... java.lang.string类型的#(mystring)无法转换为jsonarray [英] Error parsing data org.json.jsonexception: value ....#(mystring) of type java.lang.string cannot be converted to jsonarray

查看:137
本文介绍了解析数据时出错org.json.jsonexception:value .... java.lang.string类型的#(mystring)无法转换为jsonarray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在android listView中显示我的json字符串但发现错误

注意:我正在使用WCF服务将我的数据转换为json格式。



解析数据时出错org.json.JSONException:Value [{Id:1,Name:Umair,SSN:11111111},{Id:2,名称:khan,SSN:2222222}]在java.lang.String类型的JSONDataResult中无法转换为JSONArray



我的Json字符串。 ..

{JSONDataResult:[{\Id\:1,\Name \:\Umair \,\SSN \ :\ 11111111\ },{\ Id\ :2,\ Name\ :\ khan\ \ SSN\:\ 2222222 \}}}



我的Android编码:..

protected void showList(){





试试{



// myJSON = myJSON.replace(\, ');

JSONObject jsonObj =新的JSONObject(myJSON);



JArray = jsonObj.getJS ONArray(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 ,名称);

Hashmappersons.put(TAG_SSN,ssn);



ArraypersonList.add(Hashmappersons);

$ / b $ b $





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,错误+ e);

Log.e(log_tag,解析数据时出错+ e.toString());

Log.e( log_tag,失败的数据是:\ n+ listv);



// tv_Er.setText(错误:+ e);



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

}



}





public void getData(){

class GetDataJSON扩展了AsyncTask< string,> {



@Override

protected String doInBackground(String ... params){



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



// HttpPost httppost = new HttpPost(url);



//取决于您的网络服务

// httppost.setHeader(接受,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;

Strin g result = null;

try {

// HttpResponse response = httpclient.execute(httppost);

HttpResponse response = httpclient.execute( http:)



HttpEntity entity = response.getEntity();

inputStream = entity.getContent();



// HttpResponse response = httpClient.execute(request);

// HttpEntity responseEntity = response.getEntity();

/ / inputStream = responseEntity.getContent();



// json默认为UTF-8



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(例外e){

//哎呀

}

最后{

尝试{

if(inputStream!= null)inputStream.close();

}

catch(异常挤压)

{



}

}

返回结果;

}



@Override

protected void onPostExecute(String result){

myJSON = result;

showList();

}

}

GetDataJSON g = new GetDataJSON();

g.execute();

}



我尝试了什么:



i我试图在android listView中显示我的json字符串,但总是发现错误

类型java.lang.String的值无法转换为JSONArray。

解决方案

在您的Json字符串中,删除双引号,字符串将为关注



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


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 .

解决方案

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\"}]}


这篇关于解析数据时出错org.json.jsonexception:value .... java.lang.string类型的#(mystring)无法转换为jsonarray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆