Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JSON-使用数组创建JSONObject_Java_Android_Json - Fatal编程技术网

Java JSON-使用数组创建JSONObject

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

我需要在Java(Android)中创建一个JSONObject/JSONArray,其结构如下:

{
    "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"]}