JSON - 与阵列创建的JSONObject [英] JSON - Create JSONObject with array

查看:135
本文介绍了JSON - 与阵列创建的JSONObject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Java(Android版)来创建一个JSONObject / JSONArray的确切结构如下:

I need to create in a Java (Android) a JSONObject/JSONArray with the exact following structure:

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

有人可以帮我这个好吗?

Can somebody help me on this please?

编辑:

我所拥有的是这样的:

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

感谢

推荐答案

您已经有了您的数组创建有点混淆。你真正想要的是抱着字符串数组的对象,但你正在做的是创造一个数组的JSONObject

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.

试试这个:

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

这篇关于JSON - 与阵列创建的JSONObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆