Java 如何在Android中读取json数组
我在读取从服务器获取的json数组时遇到了这个问题:,这个json数组中没有json对象。如下图所示Java 如何在Android中读取json数组,java,android,arrays,json,Java,Android,Arrays,Json,我在读取从服务器获取的json数组时遇到了这个问题:,这个json数组中没有json对象。如下图所示 {"password":"raj","address":"dfsaf","state":"tamilnadu","image":"Bindu Appalam.jpg", "dept":"BE(Mech)","fname":"fasdf","mname":"saffd","dob":"14/12/1951","sex":"Male", "email":"fdasd@gmail.com","co
{"password":"raj","address":"dfsaf","state":"tamilnadu","image":"Bindu Appalam.jpg",
"dept":"BE(Mech)","fname":"fasdf","mname":"saffd","dob":"14/12/1951","sex":"Male",
"email":"fdasd@gmail.com","contact":"212121212545855555545","city":"dsfadf",
"pin":"600106","sname":"fdjsf","studid":"123789"}
我想解析它
我尝试从httpclient请求中获取json数组,如下所示:
try {
System.out.println("url tt : "+url);
// http client
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpEntity httpEntity = null;
HttpResponse httpResponse = null;
System.out.println(" Urls Input to "+url);
// Checking http request method type
if (method == POST) {
HttpPost httpPost = new HttpPost(url);
// adding post params
if (params != null) {
httpPost.setEntity(new UrlEncodedFormEntity(params));
}
httpResponse = httpClient.execute(httpPost);
} else if (method == GET) {
// appending params to url
System.out.println(" Params Valyes "+params);
if (params != null) {
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
System.out.println(" Params String "+paramString);
}
HttpGet httpGet = new HttpGet(url);
httpResponse = httpClient.execute(httpGet);
}
httpEntity = httpResponse.getEntity();
response = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
但得到以下错误响应:
05-20 16:38:25.420: I/System.out(29983):
Output <!DOCTYPE html><html><head><title>Apache Tomcat/8.0.5 - Error report
</title><style type="text/css">H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;}
H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;}
H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;}
BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;}
B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;}
P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name
{color : black;}.line {height: 1px; background-color: #525D76; border: none;}</style> </head><body><h1>HTTP Status 404 - Not Found
</h1><div class="line"></div><p><b>type</b> Status report</p><p><b>message</b> <u>Not Found</u></p><p><b>description</b>
<u>The requested resource is not available.</u></p><hr class="line"><h3>Apache Tomcat/8.0.5</h3></body></html>
05-20 16:38:25.420:I/System.out(29983):
输出ApacheTomcat/8.0.5-错误报告
H1{字体系列:Tahoma,Arial,无衬线;颜色:白色;背景色:#525D76;字体大小:22px;}
H2{字体系列:Tahoma,Arial,无衬线;颜色:白色;背景色:#525D76;字体大小:16px;}
H3{字体系列:Tahoma,Arial,无衬线;颜色:白色;背景色:#525D76;字体大小:14px;}
正文{字体系列:Tahoma,Arial,无衬线;颜色:黑色;背景色:白色;}
B{字体系列:Tahoma,Arial,无衬线;颜色:白色;背景色:#525D76;}
P{font系列:Tahoma,Arial,无衬线;背景:白色;颜色:黑色;字体大小:12px;}A{color:black;}A.name
{颜色:黑色;}。行{高度:1px;背景色:#525D76;边框:无;}HTTP状态404-未找到
键入状态报告未找到消息说明
请求的资源不可用。
Apache Tomcat/8.0.5
任何主体,请在此提供帮助…JSON对象以{开头,以}结尾,而JSON数组以[开头,以A结尾] 在您的情况下,将代码改为具有JSONObject
JSONObject json = new JSONObject(jsonString);
JSONArray jArray = json.getJSONArray("list");
System.out.println("*****JARRAY*****"+jArray.length());
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
Log.i("log_tag","_id"+json_data.getInt("account")+
", mall_name"+json_data.getString("name")+
", location"+json_data.getString("number")+
", telephone"+json_data.getString("url")+
",----"+json_data.getString("balance")+
",----"+json_data.getString("credit")+
",----"+json_data.getString("displayName")
);
}
jsonobjectjson=newjsonobject(jsonString);
JSONArray jArray=json.getJSONArray(“列表”);
System.out.println(“****JARRAY*****”+JARRAY.length());
对于(inti=0;iJSON对象以{开头,以}结尾,而JSON数组以A开头,以A结尾)
在您的情况下,将代码改为具有JSONObject
JSONObject json = new JSONObject(jsonString);
JSONArray jArray = json.getJSONArray("list");
System.out.println("*****JARRAY*****"+jArray.length());
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
Log.i("log_tag","_id"+json_data.getInt("account")+
", mall_name"+json_data.getString("name")+
", location"+json_data.getString("number")+
", telephone"+json_data.getString("url")+
",----"+json_data.getString("balance")+
",----"+json_data.getString("credit")+
",----"+json_data.getString("displayName")
);
}
jsonobjectjson=newjsonobject(jsonString);
JSONArray jArray=json.getJSONArray(“列表”);
System.out.println(“****JARRAY*****”+JARRAY.length());
对于(inti=0;i,请在线参考如何获得JSON响应
请在线查看如何获得JSON响应
在您的代码中,没有json数组,它是带有名称-值对数据的json对象。因此您必须解析JSONObject
而不是JSONArray
比如:
JSONObject json = new JSONObject(jsonString);
String password = json.getString("password");
类似地,您可以获得对象的所有值。在您的代码中,没有json数组,它是带有名称-值对数据的json对象。因此您必须解析JSONObject
而不是JSONArray
比如:
JSONObject json = new JSONObject(jsonString);
String password = json.getString("password");
类似地,您可以获取对象的所有值。看起来您在服务调用中未找到404?它无法解析它,因为找不到它。我认为您的json是错误的。您可以给我们正确的url吗?raj是双重的quote@Vijay,对不起,这是在双任务那里,我错了,将编辑它..看起来像你一个你的服务呼叫中找不到404?它无法解析它,因为找不到它。我想你的json是错误的。你能给我们正确的url吗?raj是双重的quote@Vijay,很抱歉,这是在双任务中,我错误地这样做了,将对其进行编辑..您好,我的问题在于使用httpclient从服务器获取响应。。我在这里遇到Http未找到错误..您好,请回答我的问题是在这里使用httpclient从服务器获取响应..我在这里遇到Http未找到错误。。