繁体   English   中英

如何停止 JSONObject 将我的 arrayList [3,4] 转换为字符串“[3,4]”

[英]How to stop JSONObject to convert my arrayList [3,4] in string "[3,4]"

1)这是代码:

JSONObject myJson=new JSONObject();

try {

   List<Integer> myLists = new ArrayList<>();
   myLists.add(4);
   myLists.add(10);

   myJson.put("myLists",myLists);

} catch (JSONException e) {
        e.printStackTrace();
}

Log.w("myJson", myJson);

2)这是控制台中的 output:

"myJson":"[4, 10]"

2)这就是我在控制台中想要的:

"myJson":[4, 10]                 (note the absence of quote "" around the array)

只需使用:

        myJson.put("myLists", new JSONArray(myLists));

否则,如果你传递 ArrayList,或 List,这将被识别为 Object,而不是像 Array 这样的东西,因此将使用 toString() 方法将其放入 JSON。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM