JSON错误“无法将类型为java.lang.String的结果值转换为JSONArray".在Android中 [英] JSON error "Value at result of type java.lang.String cannot be converted to JSONArray" in android

查看:87
本文介绍了JSON错误“无法将类型为java.lang.String的结果值转换为JSONArray".在Android中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了

I used the link for JSON RPC. I am getting a response as expected. But when i try to parse the response, it's giving me json error.

我的代码:

JSONEntity entity = new JSONEntity(jsonRequest);
    HttpPost request = new HttpPost("http://192.168.1.150/jsondemo12/service.asmx");
    request.setEntity(entity);
    HttpResponse response = httpClient.execute(request);
    StatusLine statusLine = response.getStatusLine();
    int statusCode = statusLine.getStatusCode();
    if (statusCode == 200) {
        HttpEntity httpEntity = response.getEntity();
        InputStream content = httpEntity.getContent();

        BufferedReader reader = new BufferedReader(
                new InputStreamReader(content,"iso-8859-1"),8);
        String line;
        while ((line = reader.readLine()) != null) {
            builder.append(line);
        }
        content.close();
    } else {
        Log.e(AndroidJSONActivity.class.toString(), "Failed to download file");
    }


    strJSONValue=builder.toString();

    txtViewParsedValue.append("\n+++++++++++++\n"+strJSONValue+"\n");
    try {
        parseJSON();
    } catch (JSONException e) {
        Log.e("error","Error while parsing!!!");
        e.printStackTrace();
    }
    Log.e("response", strJSONValue);
public void parseJSON() throws JSONException
    {
        String attr1="",attr2="";
        jsonObject = new JSONObject(strJSONValue);
        JSONArray  result = jsonObject.getJSONArray("result");    <- Error in this line!!!
        for(int i=0;i < result.length();i++){
            JSONObject e = result.getJSONObject(i);
            attr1 = "ExhibitorID: "+ e.getString("ExhibitorID");
            attr2 = "ExhibitorName: "+e.getString("ExhibitorName");
        }
        strParsedValue=attr1+"\n"+attr2;
        Log.d("Parse", attr1);
        Log.d("Parse", attr2);

        txtViewParsedValue.append("\n**********\nParsed Value: \n");
        txtViewParsedValue.append(strParsedValue);
    }

我在"strJSONValue"中得到的结果是字符串格式,没有开头和结尾的双引号. 像:

The result i get in "strJSONValue" is string format, without the starting and ending double quotes. Like:

{"id":2,"result":"[
{\"ExhibitorID\":42, etc....}
]"}

结果字符串符合要求,但我无法根据要求将字符串解析为JSON对象.它在Logcat中给出错误:org.json.JSONException: Value <content of the string> at result of type java.lang.String cannot be converted to JSONArray

The result string is as per requirement,but i am not able to parse the string into the JSON Object as per requirement. It gives error in Logcat: org.json.JSONException: Value <content of the string> at result of type java.lang.String cannot be converted to JSONArray

请帮助我. 谢谢

推荐答案

您的结果实际上返回的是字符串而不是json数组.如果您的json格式是这样,它将返回json数组

your result is actually returning a string not a json array. it will return json array if your json format would be like this

{
  "id": 2,
  "result": [
    {
      "ExhibitorID": 42
    }
  ]
}

当前为这种形式:

{
  "id": 2,
  "result": "[ {\"ExhibitorID\":42, etc....} ]"
}

这篇关于JSON错误“无法将类型为java.lang.String的结果值转换为JSONArray".在Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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