0

我正在 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 上找到了这个,但我无法理解。我怎样才能总结它们?

4

2 回答 2

0

你必须这样做

int totalAge = 0;
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");


                    totalAge = totalAge + Integer.ParseInt(age);
                    HashMap<String, String> persons = new HashMap<>();

                    persons .put("id", id);
                    persons .put("name", name);
                    persons .put("age", age);

                    personList.add(persons);

totalAge给你年龄的总和。

于 2018-12-17T11:10:38.567 回答
0

只需 U 可以使用以下代码,因为 U 能够解析 JSON 值 :)

只需全局声明一个变量并将其用于求和

private int sumOfAge = 0;
private int ageValue;

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

                ageValue = Integer.parseInt(age);
                sumOfAge+=ageValue;


                HashMap<String, String> persons = new HashMap<>();

                persons .put("id", id);
                persons .put("name", name);
                persons .put("age", age);

                personList.add(persons);
于 2018-12-17T11:14:21.297 回答