Java JSON-使用数组创建JSONObject
我需要在Java(Android)中创建一个JSONObject/JSONArray,其结构如下:Java JSON-使用数组创建JSONObject,java,android,json,Java,Android,Json,我需要在Java(Android)中创建一个JSONObject/JSONArray,其结构如下: { "listId": [ "c02bc683-fcd7-47a5-b157-853e26ed099e", "f8e1c9d7-ae45-4433-a315-726c1d912d09" ] } 有人能帮我吗 编辑: 我所拥有的是: JSONArray obj = new JSONArray(); JSONObject jsonObject
{
"listId":
[
"c02bc683-fcd7-47a5-b157-853e26ed099e",
"f8e1c9d7-ae45-4433-a315-726c1d912d09"
]
}
有人能帮我吗
编辑:
我所拥有的是:
JSONArray obj = new JSONArray();
JSONObject jsonObject = new JSONObject();
jsonObject.put("listId", folderId);
obj.put(jsonObject);
JSONObject json = new JSONObject();
json.put("id", obj);
但这会产生如下结果:
{
"listId":
[
{"id":"c02bc683-fcd7-47a5-b157-853e26ed099e"},
{"id":"f8e1c9d7-ae45-4433-a315-726c1d912d09"}
]
}
谢谢您的阵列创建有点混乱。您真正想要的是一个包含字符串数组的对象,但您所做的是创建一个
JSONObject
数组
请尝试以下方法:
String[] arr = { "c02bc683-fcd7-47a5-b157-853e26ed099e", "f8e1c9d7-ae45-4433-a315-726c1d912d09" };
JSONArray jsonArray = new JSONArray(arr);
JSONObject json = new JSONObject();
json.put("listId", jsonArray);
System.out.println(json); // {"listId":["c02bc683-fcd7-47a5-b157-853e26ed099e","f8e1c9d7-ae45-4433-a315-726c1d912d09"]}
您的数组创建有点混乱。您真正想要的是一个包含字符串数组的对象,但您所做的是创建一个
JSONObject
数组
请尝试以下方法:
String[] arr = { "c02bc683-fcd7-47a5-b157-853e26ed099e", "f8e1c9d7-ae45-4433-a315-726c1d912d09" };
JSONArray jsonArray = new JSONArray(arr);
JSONObject json = new JSONObject();
json.put("listId", jsonArray);
System.out.println(json); // {"listId":["c02bc683-fcd7-47a5-b157-853e26ed099e","f8e1c9d7-ae45-4433-a315-726c1d912d09"]}
您的数组创建有点混乱。您真正想要的是一个包含字符串数组的对象,但您所做的是创建一个
JSONObject
数组
请尝试以下方法:
String[] arr = { "c02bc683-fcd7-47a5-b157-853e26ed099e", "f8e1c9d7-ae45-4433-a315-726c1d912d09" };
JSONArray jsonArray = new JSONArray(arr);
JSONObject json = new JSONObject();
json.put("listId", jsonArray);
System.out.println(json); // {"listId":["c02bc683-fcd7-47a5-b157-853e26ed099e","f8e1c9d7-ae45-4433-a315-726c1d912d09"]}
您的数组创建有点混乱。您真正想要的是一个包含字符串数组的对象,但您所做的是创建一个
JSONObject
数组
请尝试以下方法:
String[] arr = { "c02bc683-fcd7-47a5-b157-853e26ed099e", "f8e1c9d7-ae45-4433-a315-726c1d912d09" };
JSONArray jsonArray = new JSONArray(arr);
JSONObject json = new JSONObject();
json.put("listId", jsonArray);
System.out.println(json); // {"listId":["c02bc683-fcd7-47a5-b157-853e26ed099e","f8e1c9d7-ae45-4433-a315-726c1d912d09"]}