create nested JSON object in php?

后端 未结 7 810
广开言路
广开言路 2020-12-09 16:25

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

相关标签:
7条回答
  • 2020-12-09 16:44

    Hey here is a quick trick to manually convert complex JSONs into a PHP Object.

    Grab the JSON example as you have:

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

    Search-Replace { to array(

    Search-Replace : to =>

    Search-Replace } to )

    Done.

    0 讨论(0)
  • 2020-12-09 16:55

    User array to get the correct format and then call 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"
     ))
    
    0 讨论(0)
  • 2020-12-09 16:55
    $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";
    

    Perhaps something like the above? The client would hold two objects. Information and Data.

    Edit Using json_encode, you would create this object as an array in 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);
    
    0 讨论(0)
  • 2020-12-09 16:56

    You can use json_encode to encode a php array 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);
    

    hopefully this helps..

    posted it, then seen loads of answers already! :|

    0 讨论(0)
  • 2020-12-09 17:01

    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"
         )
    ));
    

    see json_encode

    0 讨论(0)
  • 2020-12-09 17:01

    Use the in build function of PHP:

    json_encode();

    this will convert the array into JSON object.

    0 讨论(0)
提交回复
热议问题