在PHP中创建嵌套的JSON对象? [英] create nested JSON object in php?

查看:283
本文介绍了在PHP中创建嵌套的JSON对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不太使用php,并且在对象创建方面有些模糊.我需要发出发送json的网络服务请求,我想我已经覆盖了那部分.在提交数据之前,我需要创建一个嵌套对象.根据我使用基于ecma的脚本语言的经验,我认为这是微不足道的,但是我发现该语法难以导航.我要创建的对象如下.

I don't work with php much and I'm a little fuzzy on object creation. I need to make a webservice request sending json and I think I have that part covered. Before I can submit the data I need to create a nested object. I was assuming this would be trivial based on my experience with ecma based scripting languages, but I'm finding the syntax to be difficult to navigate. The object I want to create is below.

{ "client": {
    "build": "1.0",
    "name": "xxxxxx",
    "version": "1.0"
    },
    "protocolVersion": 4,
    "data": {
        "distributorId": "xxxx",
        "distributorPin": "xxxx",
        "locale": "en-US"
    }
}

我已经看到了很多平面对象的示例,但是我还没有找到嵌套对象的最小示例.上面对象的php语法是什么?这是用php做的不寻常的事情吗?

I've seen a lot of examples of flat objects, but I haven't found a minimal example for a nested object yet. What would be the php syntax for the object above? Is this an unusual thing to do in php?

推荐答案

可以通过以下PHP代码创建此JSON结构

this JSON structure can be created by following PHP code

$json = json_encode(array(
     "client" => array(
        "build" => "1.0",
        "name" => "xxxxxx",
        "version" => "1.0"
     ),
     "protocolVersion" => 4,
     "data" => array(
        "distributorId" => "xxxx",
        "distributorPin" => "xxxx",
        "locale" => "en-US"
     )
));

请参见 json_encode

这篇关于在PHP中创建嵌套的JSON对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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