This question has already been answered:

I have an array in json that looks like this:

contacts:[ { "contact_id": 1 }, { "contact_id": 2 } ] 

I take out the whole thing like this:

 public void doPost(HttpServletRequest requestObject, HttpServletResponse responseObject) throws ServletException, IOException { JsonObject requestJson = getJsonContent(requestObject); JsonArray contactsArray=requestJson.getAsJsonArray("contacts"); 

But I just can’t figure out how I can get all the values ​​of all contact_id variables so that I can search the database for each of them.

Reported as a duplicate by participants Victor , Spirit of the Community Mar 2 '18 at 15:50 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

    2 answers 2

    Try it like this.

     for (int i = 0; i < contactsArray.length(); i++) { JsonObject jsonObj = contactsArray.getJSONObject(i); //сдесь что-то делаете с определенным JsonObject } 
       //создаем json JSONObject jsonObject = new JSONObject(); jsonObject.put("contacts", new JSONArray() .put(new JSONObject().put("contacts_id", "1")) .put(new JSONObject().put("contacts_id", "2"))); JSONArray jsonArray = jsonObject.getJSONArray("contacts"); for (int i=0; i < jsonArray.length(); i++) { JSONObject jsonObject1 = jsonArray.getJSONObject(i); System.out.println(jsonObject1.getString("contacts_id")); }