2014-04-01 37 views
-4

我想在列表视图中显式地显示json响应。我如何获得在列表视图中显示json响应

DistrictName,块名称,集群名称,VillageSchools

这里是我的代码:

  { 
      DistrictId: 1, 
      DistrictName: "mys", 
      BlockSchools: [ 
      { 
      BlockId: 1, 
      BlockName: "Adoni", 
      ClusterSchools: [ 
      { 
      ClusterId: 1, 
      ClusterName: "Giddalur", 
      VillageSchools: [ 
      { 
      VillageId: 1, 
      VillageName: "Kurly", 
      VillageSchools: [ 
      { 
      SchoolId: 37, 
      SchoolName: "NORTH CHANGOUBUNG PRIMARY SCHOOL" 
      } 
      ] 
      } 
      ] 
      } 
      ], 
      VillageSchools: [ 
      { 
      VillageId: 1, 
      VillageName: "Kurly", 
      VillageSchools: [ 
      { 
      SchoolId: 37, 
      SchoolName: "NORTH CHANGOUBUNG PRIMARY SCHOOL" 
      } 
      ] 
      } 
      ] 
      } 
      ] 
      }, 

代码:

private class JSONParse 
       extends AsyncTask<String, String, ArrayList<HashMap<String, String>>> { 
    private ProgressDialog pDialog; 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     pDialog = new ProgressDialog(JsonActivity.this); 
     pDialog.setMessage("Getting Data ..."); 
     pDialog.setIndeterminate(false); 
     pDialog.setCancelable(true); 
     pDialog.show(); 
    } 

    protected ArrayList<HashMap<String, String>> doInBackground(String... args) { 
     ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String,String>>(); 
    JSONParser jParser = new JSONParser(); 

     // Getting JSON from URL 
     JSONObject json = jParser.getJSONFromUrl(url); 
     try {    
     String DistrictName = json.getString("DistrictName"); 
     System.out.println("DistrictName: "+DistrictName); 
     // tmp hashmap for single contact 
     /* HashMap<String, String> contact = new HashMap<String, String>(); 
      contact.put(TAG_NAME, DistrictName); 

      // adding contact to contact list 
      contactList.add(contact);*/ 

     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
     return contactList; 
    } 

    @Override 
    protected void onPostExecute(ArrayList<HashMap<String, String>> result) { 
     super.onPostExecute(result); 
     // Dismiss the progress dialog 

     if (pDialog.isShowing()) pDialog.dismiss(); 
      ListAdapter adapter = new SimpleAdapter(JsonActivity.this, result, R.layout.list_item, new String[] { TAG_NAME, TAG_EMAIL, TAG_PHONE_MOBILE }, new int[] 
        { R.id.textView1 }); 
      lv.setAdapter(adapter);   
     } 
    } 
} 
+1

你确定它是一个有效的JSON – Raghunandan

+0

请问你目前的尝试失败了呢? –

+0

http://119.82.102.118/eManipurSSA/Responses/Test.txt这个网址我想检索服务器的响应,我怎么能这样做 – user3114723

回答

1

您的JSON必须如下。这是因为丢失""

{ 
    "DistrictId": 1, 
    "DistrictName": "mys", 
    "BlockSchools": [ 
     { 
      "BlockId": 1, 
      "BlockName": "Adoni", 
      "ClusterSchools": [ 
       { 
        "ClusterId": 1, 
        "ClusterName": "Giddalur", 
        "VillageSchools": [ 
         { 
          "VillageId": 1, 
          "VillageName": "Kurly", 
          "VillageSchools": [ 
           { 
            "SchoolId": 37, 
            "SchoolName": "NORTH CHANGOUBUNG PRIMARY SCHOOL" 
           } 
          ] 
         } 
        ] 
       } 
      ], 
      "VillageSchools": [ 
       { 
        "VillageId": 1, 
        "VillageName": "Kurly", 
        "VillageSchools": [ 
         { 
          "SchoolId": 37, 
          "SchoolName": "NORTH CHANGOUBUNG PRIMARY SCHOOL" 
         } 
        ] 
       } 
      ] 
     } 
    ] 
} 

为了解析

通过看其相同的程序上面做解析VillageSchools。只有你有嵌套jsonarray和一个JSONObjects

"VillageSchools": [ // this is a json array 
         { // this is a json object node 
          "VillageId": 1, 
          "VillageName": "Kurly", 
          "VillageSchools": [ 
           { 
            "SchoolId": 37, 
            "SchoolName": "NORTH CHANGOUBUNG PRIMARY SCHOOL" 
           } 
          ] 
         } 
        ] 

编辑:

 JSONArray jarray = jParser.getJSONFromUrl(url); 
     for(int i=0;i<jarray.length();i++) 
     { 
     JSONObject json = jarray.getJSONObject(i); 
     String DistrictName = json.getString("DistrictName"); 
     Log.i(".............",DistrictName); 
     JSONArray jr = json.getJSONArray("BlockSchools"); 
     JSONObject jb = jr.getJSONObject(0); 
     JSONArray jr1 = jb.getJSONArray("ClusterSchools"); 
     JSONObject jb1 = jr1.getJSONObject(0); 
     String clustername = jb1.getString("ClusterName"); 
     Log.i(".............",clustername ); 
     JSONArray villageschools = jb1.getJSONArray("VillageSchools"); 
     JSONObject jsonb = villageschools.getJSONObject(0); 
     String nameschool = jsonb.getString("VillageName"); 
     Log.i(".............",nameschool); 
     JSONArray inner = jsonb.getJSONArray("VillageSchools"); 
     JSONObject innerjb = inner.getJSONObject(0); 
     String id= innerjb.getString("SchoolId"); 
     String name= innerjb.getString("SchoolName"); 
     Log.i(".............",id); 
     Log.i(".............",name); 

日志

04-01 07:40:53.527: I/.............(1525): mys 
04-01 07:40:53.527: I/.............(1525): Giddalur 
04-01 07:40:53.537: I/.............(1525): Kurly 
04-01 07:40:53.537: I/.............(1525): 37 
04-01 07:40:53.537: I/.............(1525): NORTH CHANGOUBUNG PRIMARY SCHOOL 
04-01 07:40:53.537: I/.............(1525): krishna 
04-01 07:40:53.537: I/.............(1525): chikaballapura 
04-01 07:40:53.537: I/.............(1525): TUMUYON KHULLEN 
04-01 07:40:53.537: I/.............(1525): 38 
04-01 07:40:53.537: I/.............(1525): IRANG PUBLIC JUNIOR ENGLISH SCHOOL 
+0

http://119.82.102.118/eManipurSSA/Responses/Test.txt这个网址我想检索服务器的响应,我怎么能做到这一点 – user3114723

+0

@ user3114723我发布了一个例子。我不会为你编码。如果你有一个问题的例子或有疑问,我会帮助 – Raghunandan

+0

嗨,先生,我使用该链接检索respose,如何能做到这一点 – user3114723