简体   繁体   English

JSON-使用数组创建JSONObject

[英]JSON - Create JSONObject with array

I need to create in a Java (Android) a JSONObject/JSONArray with the exact following structure: 我需要在Java(Android)中创建具有以下确切结构的JSONObject / JSONArray:

{
    "listId": 
    [
        "c02bc683-fcd7-47a5-b157-853e26ed099e",
        "f8e1c9d7-ae45-4433-a315-726c1d912d09"
    ]
}

Can somebody help me on this please? 有人可以帮我吗?

EDIT: 编辑:

What i have is this: 我所拥有的是:

JSONArray obj = new JSONArray();
JSONObject jsonObject = new JSONObject();

jsonObject.put("listId", folderId);
obj.put(jsonObject);

JSONObject json = new JSONObject();
json.put("id", obj);

But this produces something like: 但这会产生类似以下内容:

{
    "listId": 
    [
        {"id":"c02bc683-fcd7-47a5-b157-853e26ed099e"},
        {"id":"f8e1c9d7-ae45-4433-a315-726c1d912d09"}
    ]
}

Thanks 谢谢

You've got your array creation a bit mixed up. 您的数组创建有些混乱。 What you really want is an object holding an array of string, but what you're doing is creating an array of JSONObject . 您真正想要的是一个包含字符串数组的对象,但是您正在做的是创建JSONObject数组。

Try this instead: 尝试以下方法:

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

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

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