【问题标题】:How to read json array in Android如何在Android中读取json数组
【发布时间】:2014-05-20 11:44:11
【问题描述】:

我在读取从服务器获取的 json 数组时遇到了问题:http://XXX.XXX.XXX.XXX:9090/appsapi/rest/services/getStudentPersonalDetails/123789,这个 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","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>

任何机构,请在这里帮助...

【问题讨论】:

  • 看起来您在服务调用中找不到 404?它无法解析它,因为它无法找到
  • 我认为你的 json 是错误的
  • 你能给我们正确的网址吗?
  • raj 在双引号中
  • @Vijay,很抱歉,它是双排的,我弄错了,会编辑它..

标签: java android arrays json


【解决方案1】:

在您的代码中,不存在 json 数组,它是带有名称值对数据的 json 对象。所以你必须解析JSONObject 而不是JSONArray

喜欢:

JSONObject json = new JSONObject(jsonString);

String password = json.getString("password");

同样可以获取对象的所有值。

【讨论】:

  • 嗨,谢谢回复我的问题是使用 httpclient 从服务器获取响应..我到这里 Http not found 错误..
【解决方案2】:

JSON 对象以 { 开头并以 } 结尾,而 JSON 数组以 [ 开头并以 ] 结尾。

在您的情况下,将您的代码更改为使用 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")
 );

}

参考:https://stackoverflow.com/a/4244963/2771869

【讨论】:

    【解决方案3】:

    【讨论】:

      猜你喜欢
      • 2011-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-27
      • 2022-01-01
      • 1970-01-01
      • 2018-01-24
      • 1970-01-01
      相关资源
      最近更新 更多