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