Send JSON Array with key

Viewed 40
  my output "abc":[{"1":1,"2":2,"3":1,"4":2,"5":1,"6":2,"7":1,"8":2,"9":1,"10":2,"11":1}]
   
//mycode 

   JSONArray jsonArray = new JSONArray();
        JSONObject jsonObject = new JSONObject();
        for(QuizAttempt order_product:products){
            //jsonArray.put(order_product.getAns());
            jsonObject.put(order_product.getId(),order_product.getAns());
        }
        //jsonObject.put()
        jsonArray.put(jsonObject);
        paramObject.put("abc",jsonArray);

I getting out as I have shown above and above is my code but I want my output as

 "abc":[{"1":"1","2":"2","3":"1","4":"2","5":"1","6":"2","7":"1","8":"2","9":"1","10":"2","11":"1"}]

Help me to solve this problem Thank you all in advance

1 Answers

Change in object

 jsonObject.put(order_product.getId(),""+order_product.getAns());

Change in Datatype

change the datatype of getAns() to in String in your POJO file
Related