我有一个PHP多维关联数组,我想转换成JSON,里面有一个数组和一个对象

时间:2016-11-19 19:30:18

标签: php arrays json

我有一个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"}]}

我编码后可以手动更改此字符串并插入' ['和']'在需要的地方,还有另一种方式吗?

1 个答案:

答案 0 :(得分:2)

ArrayList<Issue> list = new ArrayList<>();
Issue issue = new Issue();
list.add(issue);

将数组转换为对象,再次包装数组,然后在结果上使用json_encode。

相关问题