JSON对象构建

时间:2016-10-10 13:15:00

标签: android json

目前我有这个JSON结构:

functions = [lambda: hi('John'),
             lambda: howold('20', 'John'),
             lambda: howmuch('50')]
random.shuffle(functions)

for f in functions:
    f()

我需要做的是:

{"title1":["info1","info2"],"title2":"info2","title3":"info3"}

为实现上述目标,我使用JSONArray:

{"title4":[{"a":"aa","b": "bb"}]}

和JSONObject:

JSONArray list = new JSONArray();
    list.put("info1");
    list.put("info2");
    obj.put("title1", list);

如何可视化所需的输出?

1 个答案:

答案 0 :(得分:1)

要实现您的目标,JSONObject JSONArray包含JSONObjects,您可以使用以下代码

JSONObject root = new JSONObject(); // The root JSON object

JSONArray title4 = new JSONArray(); // The JSON array that will contain JSON objects

// The JSON objects
JSONObject a = new JSONObject();
a.put("a", "aa");

JSONObject b = new JSONObject();
b.put("b", "bb");
title4.put(a);
title4.put(b);

// Put the JSON array in the root JSON object
root.put("title4", title4);