我有一个PHP数组:
(
[username] => john
[attachments] => Array
(
[text] => hello
[color] => green
)
)
我需要将其发布为JSON,所以我知道我可以json_encode它,我得到:
{"username":"john","attachments":{"text":"hello","color":"green"}}
但我发帖的地方需要附件'作为一个JSON对象和不一个数组,如下所示:
{"username":"john","attachments":[{"text":"hello","color":"green"}]}
我编码后可以手动更改此字符串并插入' ['和']'在需要的地方,还有另一种方式吗?
答案 0 :(得分:2)
ArrayList<Issue> list = new ArrayList<>();
Issue issue = new Issue();
list.add(issue);
将数组转换为对象,再次包装数组,然后在结果上使用json_encode。