首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用PHP创建JSON对象

使用PHP创建JSON对象
EN

Stack Overflow用户
提问于 2016-04-20 16:36:54
回答 2查看 70关注 0票数 0

如何使用PHP实现或创建此类型的JSON对象?

代码语言:javascript
复制
{
  "actors": [
    {
      "name": "Brad Pitt",
      "description": "William Bradley 'Brad' Pitt is an American actor and film producer. He has received a Golden Globe Award, a Screen Actors Guild Award, and three Academy Award nominations in acting categories",
      "dob": "December 18, 1963",
      "country": "United States",
      "height": "1.80 m",
      "spouse": "Jennifer Aniston",
      "children": "Shiloh Nouvel Jolie-Pitt, Maddox Chivan Jolie-Pitt",
      "image": "http://microblogging.wingnity.com/JSONParsingTutorial/brad.jpg"
    },
  ]
}
EN

回答 2

Stack Overflow用户

发布于 2016-04-20 16:43:59

正如我所说的:您必须设计一个PHP数组并使用json_encode函数。

在线编译:My answer

尝试执行以下操作:

阵列

代码语言:javascript
复制
$actors = array("actors" => array(
            array("name" => "Brad Pitt",
                  "description" => "William Bradley 'Brad' Pitt is an American actor and film producer. He has received a Golden Globe Award, a Screen Actors Guild Award, and three Academy Award nominations in acting categories",
                  "dob" => "December 18, 1963",
                  "country" => "United States",
                  "height" => "1.80 m",
                  "spouse" => "Jennifer Aniston",
                  "children" => "Shiloh Nouvel Jolie-Pitt, Maddox Chivan Jolie-Pitt",
                  "image" => "http://microblogging.wingnity.com/JSONParsingTutorial/brad.jpg"
            ))
        );

编码

代码语言:javascript
复制
echo $out = json_encode($actors);

结果:

代码语言:javascript
复制
{"actors":[{"name":"Brad Pitt","description":"William Bradley 'Brad' Pitt is an American actor and film producer. He has received a Golden Globe Award, a Screen Actors Guild Award, and three Academy Award nominations in acting categories","dob":"December 18, 1963","country":"United States","height":"1.80 m","spouse":"Jennifer Aniston","children":"Shiloh Nouvel Jolie-Pitt, Maddox Chivan Jolie-Pitt","image":"http:\/\/microblogging.wingnity.com\/JSONParsingTutorial\/brad.jpg"}]}
票数 0
EN

Stack Overflow用户

发布于 2016-04-20 16:44:06

您需要创建一个包含数据的数组,并使用json_encode将其转换为JSON字符串。

代码语言:javascript
复制
$myData = [
    "actors" => [
        [
            "name" => "Brad Pitt",
            "description" => "William Bradley 'Brad' Pitt is an American actor and film producer. He has received a Golden Globe Award, a Screen Actors Guild Award, and three Academy Award nominations in acting categories",
            "dob" => "December 18, 1963",
            "country" => "United States",
            "height" => "1.80 m",
            "spouse" => "Jennifer Aniston",
            "children" => "Shiloh Nouvel Jolie-Pitt, Maddox Chivan Jolie-Pitt",
            "image" => "http://microblogging.wingnity.com/JSONParsingTutorial/brad.jpg"
        ]
    ]
];

$myJSON = json_encode($myData);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36738209

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档