gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 22:09:42 26 4
gpt4 key购买 nike

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

我的实验 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();
}
}

最佳答案

希望这对您有帮助

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

关于java - 如何将 JSON 字符串解析到 ListView 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58667745/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com