DJ_AM_Juicebox
asked on
Return JSON from php script
Hi,
I have a PHP script. I want to output a result as a JSON string/object. I'd like the output to look like this:
{
"message": {
"text": "Students fetched ok!",
},
"students": [
{
"student_id": "123",
"student_name": "john doe"
},
{
"student_id": "456",
"student_name": "jane doe"
}
]
}
not sure where to get started though. Looks like this is one big JSON object, which has a 'message' object, then an array of students (which I'll fetch from a database). What's a standard way people generate these types of responses from php scripts?
Thanks
I have a PHP script. I want to output a result as a JSON string/object. I'd like the output to look like this:
{
"message": {
"text": "Students fetched ok!",
},
"students": [
{
"student_id": "123",
"student_name": "john doe"
},
{
"student_id": "456",
"student_name": "jane doe"
}
]
}
not sure where to get started though. Looks like this is one big JSON object, which has a 'message' object, then an array of students (which I'll fetch from a database). What's a standard way people generate these types of responses from php scripts?
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
in php encoding json objects its really easy, you only need to:
echo json_encode($yourdata);
and php will convert your array in json format
for more info: php.net/json_encode