解析来自url帮助所需的JSON值

时间:2013-07-24 11:21:23

标签: android json

在我的代码中json vlaue没有显示帮助我worldpopulation vlaues wil show on screen但是worldpopulation2值没有显示我的代码中的错误是什么?检查我的xml文件请帮我检查一下我的代码为什么worldpopulation1值显示y worldpopulation2值不显示???

         CategoryAPI = Utils.CategoryAPI2;


    try {

        HttpClient client = new DefaultHttpClient();
        HttpConnectionParams.setConnectionTimeout(client.getParams(), 
      15000);
        HttpConnectionParams.setSoTimeout(client.getParams(), 15000);
        HttpUriRequest request = new HttpGet(CategoryAPI);
        HttpResponse response = client.execute(request);
        InputStream atomInputStream = response.getEntity().getContent();
        BufferedReader in = new BufferedReader(new 
               InputStreamReader(atomInputStream));

        String line;
        String str = "";
        while ((line = in.readLine()) != null){
            str += line;
        }


             JSONObject json = new JSONObject(str);
            JSONArray data = json.getJSONArray("worldpopulation");

            JSONObject person=(new JSONObject("worldpopulation2"));
             String Name=person.getString("describtion");
             String url2=person.getString("url");
             txtdescription.setText("json values are"+Name+""+url2);

            for (int i = 0; i < data.length(); i++) {
                JSONObject object = data.getJSONObject(i); 



                  Category_ID.add(Long.parseLong(object.getString("rank")));
                 Category_name.add(object.getString("name"));
                 Category_image.add(object.getString("url"));

                Log.d("Category name", Category_name.get(i));
                 listview.setAdapter(cla);


             }


    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
    //  IOConnect = 1;
        e.printStackTrace();
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
     }  



}






                  {
"worldpopulation": [
{
 "rank":1,
 "name": "Angelina",
  "url": "http://www.bounty4u.com/android/images/angie.jpg"
 },
 {   
 "rank":2,
 "name": "Ashton ",
  "url": "http://www.bounty4u.com/android/images/ashton.jpg"
 },
 {  
 "rank":3,
 "name": "Jackman",
  "url": "http://www.bounty4u.com/android/images/hugh.jpg"
}

]
,
"worldpopulation2": 
{
"rank":1,
"describtion": "Super mazaydar biryani",
 "url": "http://2.bp.blogspot.com/_JU_j7jj5TjU/TSBQKRukf1I/AAAAAAAAAs8/X1w5_z6pjwQ   
 /s1600/chicken-biryani.jpg"
 }



   }

1 个答案:

答案 0 :(得分:0)

而不是

JSONArray data = json.getJSONArray("worldpopulation");

JSONObject person=(new JSONObject("worldpopulation2"));

执行类似这样的操作,从响应JSON数组创建新的jsonObject。

 JSONArray data = json.getJSONArray("worldpopulation");

现在在JSONArray数据上运行一个循环,看看你想从JSON对象中获取什么。

 for (int i = 0; i < data.length(); i++) {
    JSONObject person = data.getJSONObject(i);
    String population2 = person.getString("worldpopulation2");
   }