【问题标题】:Send JSON Array with key发送带键的 JSON 数组
【发布时间】:2021-02-15 22:06:32
【问题描述】:
  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"}]

帮我解决这个问题 先谢谢大家了

【问题讨论】:

  • jsonObject.put(order_product.getId(),""+order_product.getAns()); 或在 pojo 文件中将 getAns() 的数据类型更改为字符串
  • 还有一件事是有任何其他选项可以用json数组中的键发送值???
  • @DeepParsania 我还有一个查询,请查看我的输出它正在发送数组中的对象我不必发送对象我必须像这样发送 "abc":["1":"1 ","2":"2","3":"1","4":"2","5":"1","6":"2","7":"1", "8":"2","9":"1","10":"2","11":"1"] 我该怎么做
  • 可以用HashMap代替jsonObject Here tutorial for it
  • @DeepParsania 它给了我这个 "abc":["{44=2, 45=1, 42=1, 43=2}"]} 但我想要 "abc":["44 ":"2","45":"1"]

标签: android arrays json android-studio


【解决方案1】:

对象变化

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

数据类型变化

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

【讨论】:

  • 感谢您的解决方案。我在上面的评论中又发布了一个查询,请回答。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-16
  • 1970-01-01
  • 1970-01-01
  • 2016-10-06
  • 1970-01-01
相关资源
最近更新 更多