我用的是link http://p-xr.com/android-tutorial-how-to-parse-read-json-data-into-a-android-listview/对于 JSON RPC。我得到了预期的答复。但是当我尝试解析响应时,它给了我 json 错误。
My code:
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”中得到的结果是字符串格式,没有开始和结束双引号。
喜欢:
{"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
请帮我。
谢谢
你的结果实际上返回一个字符串而不是 json 数组。如果你的 json 格式是这样的,它将返回 json 数组
{
"id": 2,
"result": [
{
"ExhibitorID": 42
}
]
}
目前它是这种形式:
{
"id": 2,
"result": "[ {\"ExhibitorID\":42, etc....} ]"
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)