简体   繁体   中英

How to parse a JSON string to ListView?

So I am beginning programming Java Android and I'm trying to parse a JSON string that I created. So, I want to parse it to a ListView and I need people to help me.

My experimental JSON file:

[ 
   { 
      "HoTen":" Nguy\u1ec5n V\u0103n A",
      "NamSinh":1999,
      "DiaChi":"H\u00e0 N\u1ed9i"
   },
   {+},
   {+},
   {+},
   {+},
   {+},
   {+},
   {+},
   {+}
]

My code but it not working:

 protected void onPostExecute(String s) {

     //Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG).show();
     try {
         mangLV = new ArrayList<String>();

         JSONArray jsonArray = new JSONArray(s);
         JSONObject jsonObject = new JSONObject(s);
         for (int i =0;i<=jsonObject.length();i++)
         {
             JSONObject  object = jsonArray.getJSONObject(i);
             //HoTen.getString("HoTen");
             String HoTen = object.getString("HoTen");
             int NamSinh = object.getInt("NamSinh");
             String DiaChi = object.getString("DiaChi");

         }
         ArrayAdapter adapter = new ArrayAdapter(getApplicationContext(),android.R.layout.simple_list_item_1,mangLV);
         lvSinhVien.setAdapter(adapter);
     } catch (JSONException e) {
         e.printStackTrace();
     }
 }

It doesn't seem like you're using your JSONObject jsonObject = new JSONObject(s); . Your for should be for(int i = 0; i<=jsonArray.length();i++) , thus making your jsonObject obsolete. Also, you're passing your adapter an empty list! You never add anything to your mangLV list.

Try out this code:

    protected void onPostExecute(String s) {
        //Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG).show();
        try {
            mangLV = new ArrayList<String>();

            JSONArray jsonArray = new JSONArray(s);
            for(int i = 0; i < jsonArray.length(); i++)
            {
                JSONObject object = jsonArray.getJSONObject(i);
                String HoTen = object.getString("HoTen");
                int NamSinh = object.getInt("NamSinh");
                String DiaChi = object.getString("DiaChi");
                String result = "HoTen " + HoTen + " | NamSinh " + NamSinh + " | DiaChi " + DiaChi;
                mangLV.add(result);  //After fetching the values, add the objects to your list    
            }
            ArrayAdapter adapter = new ArrayAdapter(getApplicationContext(),android.R.layout.simple_list_item_1,mangLV);
            lvSinhVien.setAdapter(adapter);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

This should add to your ListView objects that look like this:

HoTen Nguy\ễn V\ăn A | NamSinh 1999 | DiaChi H\à N\ội

Hope this will help you

protected void onPostExecute(String s) {

    //Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG).show();
    try {
        ArrayList<String> mangLV = new ArrayList<String>();

        JSONArray jsonArray = new JSONArray(s);
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject object = jsonArray.getJSONObject(i);
            //HoTen.getString("HoTen");
            String HoTen = object.getString("HoTen");
            int NamSinh = object.getInt("NamSinh");
            String DiaChi = object.getString("DiaChi");
            String result = String.format("HoTen: %s, NamSinh: %s, DiaChi: %s",
                    HoTen, NamSinh, DiaChi);
            mangLV.add(result);
        }
        ArrayAdapter adapter = new ArrayAdapter(getApplicationContext(), android.R.layout.simple_list_item_1, mangLV);
        lvSinhVien.setAdapter(adapter);
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM