Java 在android中创建JSONObject

Java 在android中创建JSONObject,java,android,Java,Android,我需要像这样形成一个JSON对象 { "GroupID": 24536, "Section": [1,2,3,4,5] } 这是我尝试过的,但是当我查看我的对象结构时,节数组没有正确形成 JSONObject Object = new JSONObject(); Object.put("Group", GroupID); int[] section = {1,2,3,4,5}; Object.put("Section", section); 您需要使用JSONArray插入一

我需要像这样形成一个JSON对象

{
    "GroupID": 24536,
    "Section": [1,2,3,4,5]
}
这是我尝试过的,但是当我查看我的对象结构时,节数组没有正确形成

JSONObject Object = new JSONObject();
Object.put("Group", GroupID);
int[] section = {1,2,3,4,5};
Object.put("Section", section);

您需要使用
JSONArray
插入一组表示数组的值,在本例中是intarray



现在,在strJson
strJson
中有了json字符串,您需要使用
JSONArray
插入表示数组的一组值,在本例中为int数组


现在,您已经在strJson
json

中找到了json字符串,请尝试:

JSONObject Object = new JSONObject();
Object.put("Group", GroupID);
int[] section = {1,2,3,4,5};
JSONArray arr = new JSONArray();
arr.put(section);
Object.put("Section", arr);
或创建集合并将其设置为值:

Collection c = Arrays.asList(section);
Object.put("Section", c);
尝试:

或创建集合并将其设置为值:

Collection c = Arrays.asList(section);
Object.put("Section", c);
尝试:

尝试:


谢谢布兰基。。。。为什么我的案子不起作用。。。为什么arraylist会起作用呢?它们都不起作用,因为它仍然是“Section”:[“java.lang.int”]谢谢branky。。。。为什么我的案子不起作用。。。为什么arraylist会起作用呢?它们都不起作用,因为它仍然是“节”:[“java.lang.int”]
    JSONObject Object = new JSONObject();
    Object.put("Group", GroupID);
    Integer[] section = {1,2,3,4,5};
    Object.put("Section", new JSONArray(Arrays.asList(section)));