简体   繁体   English

我想将 JSONObject 放入 JSONArray 内 for 循环

[英]I want to put JSONObject into JSONArray inside for loop

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

what i expect:我的期望:

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

what i am getting:我得到了什么:

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

Instead jsonArray.put can you try with jsonArray.add相反 jsonArray.put 你可以尝试使用 jsonArray.add

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

Output: Output:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Servlet内的JSONObject中放置对象的JSONArray - How to put JSONArray of object in JSONObject inside servlet Java ,将 JsonObject 放入 JsonArray,但 JsonArray 在 for 循环结束时取相同的值 - Java , put JsonObject into JsonArray, but JsonArray take the same value at the end of the for loop 尝试将JSONObject放入JsonArray时,在for循环条件中覆盖JsonObject - while trying to put JSONObject into JsonArray Override the JsonObject in for loop condition 如何在 Spring Boot 中的 JSONObject 内的 JSONArray 中放置 2 个或更多值? - How do I put 2 or more values in a JSONArray that is inside a JSONObject in Spring Boot? 如何遍历保存到 JSONObject 的数据数组并将其放入 JSONArray? - How to loop over an array of data that saved to a JSONObject and put it in a JSONArray? JSONArray中的JSONObject - JSONObject inside JSONArray JsonArray 在 Java 的 for 循环中仅采用最后一个 JsonObject - JsonArray taking only last JsonObject inside a for loop in Java 我已经将我的jsonObject转换为JsonArray,我该如何循环它 - I have Converted my jsonObject into JsonArray how can i loop it 将嵌套的JSONObject转换为JSONArray,然后将其放入Java中的HashMap中 - Convert Nested JSONObject to JSONArray and then PUT it into HashMap in java 在没有密钥的情况下在JSONArray中迭代/获取JSONObject - Iterating/Getting JSONObject inside of JSONArray without key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM