如何将 JSON 字符串解析到 ListView 中?

所以我开始编写 Java Android 并尝试解析JSON我创建的字符串。所以,我想将其解析为 aListView并且我需要人们帮助我。


我的实验 JSON 文件:


   { 

      "HoTen":" Nguy\u1ec5n V\u0103n A",

      "NamSinh":1999,

      "DiaChi":"H\u00e0 N\u1ed9i"

   },

   {+},

   {+},

   {+},

   {+},

   {+},

   {+},

   {+},

   {+}

]

我的代码但它不起作用:


 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();

     }

 }


慕田峪7331174
浏览 94回答 2
2回答

莫回无

希望对你有帮助protected void onPostExecute(String s) {&nbsp; &nbsp; //Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG).show();&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; ArrayList<String> mangLV = new ArrayList<String>();&nbsp; &nbsp; &nbsp; &nbsp; JSONArray jsonArray = new JSONArray(s);&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < jsonArray.length(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONObject object = jsonArray.getJSONObject(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //HoTen.getString("HoTen");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String HoTen = object.getString("HoTen");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int NamSinh = object.getInt("NamSinh");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String DiaChi = object.getString("DiaChi");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String result = String.format("HoTen: %s, NamSinh: %s, DiaChi: %s",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HoTen, NamSinh, DiaChi);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mangLV.add(result);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; ArrayAdapter adapter = new ArrayAdapter(getApplicationContext(), android.R.layout.simple_list_item_1, mangLV);&nbsp; &nbsp; &nbsp; &nbsp; lvSinhVien.setAdapter(adapter);&nbsp; &nbsp; } catch (JSONException e) {&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; }}

守着星空守着你

看来你没有使用你的JSONObject jsonObject = new JSONObject(s);. 你for应该是for(int i = 0; i<=jsonArray.length();i++),从而使你的 jsonObject 过时。另外,您向适配器传递了一个空列表!你永远不会在你的 mangLV 列表中添加任何东西。试试这个代码:&nbsp; &nbsp; protected void onPostExecute(String s) {&nbsp; &nbsp; &nbsp; &nbsp; //Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG).show();&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mangLV = new ArrayList<String>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONArray jsonArray = new JSONArray(s);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int i = 0; i < jsonArray.length(); i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONObject object = jsonArray.getJSONObject(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String HoTen = object.getString("HoTen");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int NamSinh = object.getInt("NamSinh");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String DiaChi = object.getString("DiaChi");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String result = "HoTen " + HoTen + " | NamSinh " + NamSinh + " | DiaChi " + DiaChi;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mangLV.add(result);&nbsp; //After fetching the values, add the objects to your list&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ArrayAdapter adapter = new ArrayAdapter(getApplicationContext(),android.R.layout.simple_list_item_1,mangLV);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lvSinhVien.setAdapter(adapter);&nbsp; &nbsp; &nbsp; &nbsp; } catch (JSONException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }这应该添加到您的 ListView 对象中,如下所示:HoTen Nguy\u1ec5n V\u0103n A | NamSinh 1999 | DiaChi H\u00e0 N\u1ed9i
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java