首页 > 解决方案 > 发送带键的 JSON 数组

问题描述

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

如上所示,我出去了,上面是我的代码,但我希望我的输出为

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

帮我解决这个问题提前谢谢大家

标签: androidarraysjsonandroid-studio

解决方案


改变对象

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

数据类型的变化

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

推荐阅读