php - 从一种 json 格式转换为另一种格式 [英] php - converting from one json format to another

查看:48
本文介绍了php - 从一种 json 格式转换为另一种格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 php 从 mysql 数据库中获取数据,并对获取的输出进行 json 编码

json_encode($data)

结果数据格式如下:

<预><代码>[{名称:aaa"},{名称:bb"}]

我想把它放到一个更大的 json 对象中,并在网络浏览器中以下列格式输出:

<代码>{内容:[{名称:aaa"},{名称:bb"}]}

我如何使用 php 执行此操作?

解决方案

如评论中所写,您可以执行 json_encode(array('content' => $data)).您还可以动态创建数组,并使用包含整个数组的变量提供 json_encode.

I am using php to fetch data from mysql database and json encoding the fetched output

json_encode($data)

The resultant data is in the form:

[
    {
        name: "aaa"
    },
    {
        name: "bbb"
    }
]

I want to put this into a bigger json object and output in the following format in the web browser:

{
    content:[
        {
            name: "aaa"
        },
        {
            name: "bbb"
        }
    ]
}

How do i do this using php?

解决方案

As written in the comment you can just do json_encode(array('content' => $data)). You can also dynamicly create the array and just feed json_encode with a variable containing the whole array.

这篇关于php - 从一种 json 格式转换为另一种格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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