Convert PHP Object To JSON

Source: Internet
Author: User
Define a class: {code...}. The JSON format I want to generate is similar to: {code...}. The UserBean contains an uncertain number of objects... How to generate such JSON data ...? Is there anything like List & #039; & amp; lt; & #039; T & #039; & amp; gt; & #039; in JAVA .... Define a class:

class UserBean {    public $user_id = 0;    public $user_name = 'null';        function setUser_id ($user_id) {        $this->user_id = $user_id;    }    function setUser_name ($user_name) {        $this->user_name = $user_name;    }        function getUser_id () {        return $this->user_id;    }    function getUser_name () {        return $this->user_name;    }}

In this case, the JSON format to be generated is similar:

{"Info": "read the data succeed", "UserBean": [{"user_id": "21", "user_name": "James "},{": [{"user_id": "23", "user_name": "Xiaodong"}]}

Under UserBean, there are uncertain objects...
How to generate such JSON data ...?
Is there anything like list' <'T'> 'in JAVA...
Please help me...

Reply content:

Define a class:

class UserBean {    public $user_id = 0;    public $user_name = 'null';        function setUser_id ($user_id) {        $this->user_id = $user_id;    }    function setUser_name ($user_name) {        $this->user_name = $user_name;    }        function getUser_id () {        return $this->user_id;    }    function getUser_name () {        return $this->user_name;    }}

In this case, the JSON format to be generated is similar:

{"Info": "read the data succeed", "UserBean": [{"user_id": "21", "user_name": "James "},{": [{"user_id": "23", "user_name": "Xiaodong"}]}

Under UserBean, there are uncertain objects...
How to generate such JSON data ...?
Is there anything like list' <'T'> 'in JAVA...
Please help me...

{"Info": "read the data succeed", "UserBean": [{"user_id": "21", "user_name": "James "},{": [{"user_id": "23", "user_name": "Xiaodong"}]}

PHP implementation: Convert the code into an array and encode it in json format.

$data['info'] = '';$data['UserBean'][0]['user_id'] = '';$data['UserBean'][0]['user_name'] = '';$data['UserBean'][1]['user_id'] = '';$data['UserBean'][1]['user_name'] = '';exit(json_encode($data));

You can query the UserBean result set directly from the database.
You can also assemble arrays by yourself.

Positive Solution:

$ Data ['info'] = 'sudoed'; $ data ['userbean'] [0] ['user _ id'] = '1 '; $ data ['userbean'] [0] ['user _ name'] = 'xiaoming '; $ data ['userbean'] [1] ['user _ id'] = '2 '; $ data ['userbean'] [1] ['user _ name'] = 'chestnut '; echo json_encode ($ data );
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.