如何在PHP中创建此JSON? [英] How to create this JSON in PHP?

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

问题描述

对不起,标题不好,但是我不知道如何在PHP中创建以下JSON:

Sorry for the bad title, but I don't know how to create following JSON in PHP:

{ 
   "id":"1",
   "method":"getData",
   "params":{ 
      "options":{ 
         "element":{ 
            "id":"1a_ext",
            "type":1,
            "keyType":"externalkey"
         },
         "moreInfo":true,
         "userFields":[ 
            "id",
            "name",
            "longname",
            "externalkey"
         ]
      }
   },
   "jsonrpc":"2.0"
}

我不知道在"params"之后做那部分(如何将选项放入" params)-对于其他部分,我知道我该怎么做:

I don't know to do the part after "params" (how do I "put" options "into" params) - for the other parts I know what I have to do:

public static function getData(){
            $json = array(
                "id" => self::id(),
                "method" => "getData",
                "params" => array(
                    "id" => self::$userid,
                    "type" => self::$type
                ),
                "jsonrpc" => "2.0"
            );
            $json = json_encode($json, true);
            return self::request($json);
        }

非常感谢您的帮助,谢谢!

I would really appreciate your help, thanks!

推荐答案

您可以使用PHP创建数组格式的this,然后进行JSON编码:

You can create the this in array format in PHP and then JSON encode:

$arr = [
    'id' => 1,
    'method' => 'getData',
    'params' => [
        'options' => [
            'element' => [
                'id' => '1a_ext',
                'type' => 1,
                'keyType' => 'externalKey'
            ],
            'moreInfo' => true,
            'userFields' => [
                'id',
                'name',
                'longname',
                'externalKey'
            ]
        ]
    ],
    'jsonrpc' => '2.0'
];

$json = json_encode($arr);

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

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