在列表视图中显示json响应

时间:2014-04-01 10:47:41

标签: android json

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

  

DistrictName,BlockName,ClusterName,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 个答案:

答案 0 :(得分: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"
                        }
                    ]
                }
            ]
        }
    ]
}

解析

JSONObject json = jParser.getJSONFromUrl(url);
String DistrictName = json.getString("DistrictName");
JSONArray jr = json.getJSONArray("BlockSchools");
JSONObject jb = jr.getJSONObject(0);
JSONArrray jr1 = jb.getJSONArray("ClusterSchools");
JSONObject jb1 = jr1.getJSONObject(0);
String clustername = jb1.getString("ClusterName");

通过查看上面的相同程序来解析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