【问题标题】:create nested JSON object in php?在 php 中创建嵌套的 JSON 对象?
【发布时间】:2013-03-26 11:18:39
【问题描述】:

我不经常使用 php,而且我对对象创建有点模糊。我需要发出一个发送 json 的网络服务请求,我想我已经涵盖了这部分。在我提交数据之前,我需要创建一个嵌套对象。根据我对基于 ecma 的脚本语言的经验,我假设这将是微不足道的,但我发现语法难以导航。我要创建的对象如下。

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

我见过很多平面对象的示例,但我还没有找到嵌套对象的最小示例。上面对象的 php 语法是什么?这在php中是不是很不寻常?

【问题讨论】:

    标签: php json


    【解决方案1】:

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

    $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

    【讨论】:

      【解决方案2】:

      嘿,这里是手动将复杂 JSON 转换为 PHP 对象的快速技巧。

      照样获取 JSON 示例:

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

      搜索-替换 {array(

      搜索-替换 :=>

      搜索-替换 })

      完成。

      【讨论】:

      • 这是一种冒险的方法。您可能会无意中替换字符,即。 {"version":"1.0:beta"} 将转换为 array("version"=>"1.0=>beta")。此外,您打算如何处理转换后的字符串?如果您打算eval() 它,那可能非常危险,特别是如果输入是由不可靠的来源提供的,请参阅stackoverflow.com/questions/951373/when-is-eval-evil-in-php
      【解决方案3】:

      用户数组获取正确格式后调用 echo json_encode(array)

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

      【讨论】:

        【解决方案4】:
        $client = new Client();
        $client->information = new Information();
        $client->information->build = '1.0';
        $client->information->name = 'xxxxxx';
        $client->information->version = '1.0';
        $client->protocolVersion = 4;
        $client->data = new Data();
        $client->data->distributorId = "xxxx";
        $client->data->distributorPin = "xxxx";
        $client->data->locale = "en-US";
        

        也许像上面那样?客户端将持有两个对象。信息和数据。

        编辑 使用 json_encode,您可以在 PHP 中将此对象创建为数组。

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

        【讨论】:

        • 我还需要创建客户端类型吗?
        • 是的,对于动态创建绝对是stdClass。
        【解决方案5】:

        我们也可以构造嵌套数组,然后做一个json_encode构造嵌套JSON。

        例如:

        {"User":
               {"username":"test",
                "address":"Posted value fro address field",
                "location":{
                             "id":12345
                            }
                }
        }
        

        上面的输出我们可以通过编写下面的php代码来实现:

        <?php
        $obj = array(
                    'username'=>$lv_username,
                    'address'=>$lv_address,
                    'location'=>array('id'=>$lv_locationId)
            );
        $data = '{"User":'. json_encode($obj) .'}';
        echo $data;
        
        ?>
        

        希望对你有帮助。

        【讨论】:

          【解决方案6】:

          使用 PHP 的 in build 函数:

          json_encode();

          这会将数组转换为 JSON 对象。

          【讨论】:

            【解决方案7】:

            你可以使用 json_encode 来编码一个 php 数组 http://php.net/manual/en/function.json-encode.php

            $theArray = array('client'= array('build'=>'1.0', 
                                            'name'=>'xxxxx', 
                                            'version'=>'1.0'
                                           ), 
                            'protocolVersion'=> 4, 
                            'data'=> array('distributorId'=>'xxxx', 
                                           'distributorPin'=>'xxxx', 
                                           'locale'=>'en-US' 
                                           ) 
                            );
            
            $theObj = json_encode($theArray);
            

            希望这会有所帮助..

            发布它,然后已经看到了很多答案! :|

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2015-07-21
              • 1970-01-01
              • 1970-01-01
              • 2017-02-14
              • 2017-12-22
              • 1970-01-01
              • 2021-10-02
              • 1970-01-01
              相关资源
              最近更新 更多