我正在 Android 中尝试 JSON Parse。我可以解析值,但我有问题。例如,我使用这个JSON。
我做了这个功能:
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
JSONArray person = jsonObj.getJSONArray("person ");
for (int i = 0; i < person .length(); i++) {
JSONObject c = person .getJSONObject(i);
String id = c.getString("id");
String name= c.getString("name");
String age= c.getString("age");
HashMap<String, String> persons = new HashMap<>();
persons .put("id", id);
persons .put("name", name);
persons .put("age", age);
personList.add(persons);
并用于显示我的屏幕:
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (pDialog.isShowing())
pDialog.dismiss();
ListAdapter adapter = new SimpleAdapter(
Mainlayout.this, personList,
R.layout.person_list, new String[]{"name", "age"},
new int[]{
R.id.name,R.id.age});
lv.setAdapter(adapter);
}
我可以在我的 ListView 中看到所有数据,但我想总结年龄。我在 StackOverflow 上找到了这个,但我无法理解。我怎样才能总结它们?