【问题标题】:I want to put JSONObject into JSONArray inside for loop我想将 JSONObject 放入 JSONArray 内 for 循环
【发布时间】:2021-05-09 12:23:43
【问题描述】:
  try {
        jsonObjectt.put("senderId", "XYZ");
        jsonObjectt.put("User_Type", usertype);
        jsonObjectt.put("Text_message", message);


           for(int i=0; i<=selectedUserIdListArray.size(); i++) {
              try { 
                  jsonObject.put("Stud_ID", selectedUserIdListArray.get(i));
                  jsonObject.put("Mobile_No", selectedUserNoListArray.get(i));
                  jsonArray.put(jsonObject);
              }catch (Exception e){
                  e.printStackTrace();
              }
           }
          jsonObjectt.put("studData",jsonArray);      


    } catch (JSONException e) {
        e.printStackTrace();
    }

我的期望:

   {
      "senderId":"XYZ",
      "User_Type":"student",
      "Text_message":"jgz ",
      "studData": [{
                    "Stud_ID":"100S1000","Mobile_No":"123456789"
                   },
                   {
                    "Stud_ID":"100S1010","Mobile_No":"321654987"
                   }]
    }

我得到了什么:

   {
      "senderId":"XYZ",
      "User_Type":"student",
      "Text_message":"jgz ",
      "studData": [{
                    "Stud_ID":"100S1000","Mobile_No":"123456789"
                   },
                   {
                    "Stud_ID":"100S1000","Mobile_No":"123456789"
                   }]
    }

【问题讨论】:

  • @patil 你为什么要那个?直接将你的 json 转换成 pojo,让你的生活变得轻松。

标签: java android arrays json parsing


【解决方案1】:

您可以尝试使用 jsonArray.add 代替 jsonArray.put

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class JSONPractise {
    public static void main(String[] args) {
        JSONObject jsonObjectt = new JSONObject();
        List<Student> selectedUserIdListArray = new ArrayList<>();
        selectedUserIdListArray.add(new Student("100S1000", "1234567890"));
        selectedUserIdListArray.add(new Student("100S1010", "3216549870"));
        JSONArray jsonArray = new JSONArray();
        try {
            jsonObjectt.put("senderId", "XYZ");
            jsonObjectt.put("User_Type", "student");
            jsonObjectt.put("Text_message", "xzf");


            for(int i=0; i<selectedUserIdListArray.size(); i++) {
                try {
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("Stud_ID", selectedUserIdListArray.get(i).getId());
                    jsonObject.put("Mobile_No", selectedUserIdListArray.get(i).getPhoneNumber());
                    jsonArray.add(jsonObject);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
            jsonObjectt.put("studData",jsonArray);

            System.out.println(jsonObjectt);
        }catch (Exception e)
        {
            System.out.println(e);
        }
    }
}

输出:

{
   "senderId":"XYZ",
   "studData":[
      {
         "Mobile_No":"1234567890",
         "Stud_ID":"100S1000"
      },
      {
         "Mobile_No":"3216549870",
         "Stud_ID":"100S1010"
      }
   ],
   "Text_message":"xzf",
   "User_Type":"student"
}

【讨论】:

    猜你喜欢
    • 2023-03-30
    • 2020-01-23
    • 2012-08-02
    • 1970-01-01
    • 2013-07-26
    • 1970-01-01
    • 2021-08-20
    • 1970-01-01
    • 2020-03-22
    相关资源
    最近更新 更多