How can I create following type JSON object using PHP? I want this JSON for my Android app.
{
"grade 3": [
{
"section": "A",
"img_url": "http://lyceumssports.com/Chrysanthemum.jpg"
},
{
"section": "B",
"img_url": "http://lyceumssports.com/Penguins.jpg"
},
{
"section": "C",
"img_url": "http://lyceumssports.com/Penguins.jpg"
}
],
"grade 2": [
{
"section": "A",
"img_url": "http://lyceumssports.com/Chrysanthemum.jpg"
},
{
"section": "B",
"img_url": "http://lyceumssports.com/Penguins.jpg"
},
{
"section": "C",
"img_url": "http://lyceumssports.com/Penguins.jpg"
}
],
"grade 1": [
{
"section": "CD 1",
"img_url": "http://lyceumssports.com/Chrysanthemum.jpg"
},
{
"section": "CD 2",
"img_url": "http://lyceumssports.com/Penguins.jpg"
}
]
}
Here I want JSON object having arrays with keys an that array also has JSON Objects.
Use a php function called json_encode($array)
. Pass the array into the function and it will give you the json object.
If you want to print JSON object beautifully then add an extra argument JSON_PRETTY_PRINT
, like json_encode($json, JSON_PRETTY_PRINT);
. Store this object into a variable and use that variable. On the other side you need to decode the JSON object, like in php,
$json_object=json_encode($json, JSON_PRETTY_PRINT);
$json_decode=json_decode($json_object);
For more details see this link, JSON.