create nested JSON object in php?

后端 未结 7 821
广开言路
广开言路 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 17:08

    We can also construct nested array and then do a json_encode to construct nested JSON.

    For e.g:

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

    Above output we can achieve by writing below php code:

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

    Hope it helps.

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