by
KK » Sun May 18, 2014 2:51 pm
Hi Said,
Following is the code I came up with to output the
JSON format you specified
- Code: Select all
<cms:content_type 'application/json'/>
{
"countries" :
[
<!-- list countries -->
<cms:set my_country_count='0' />
<cms:folders masterpage='malls.php' order='desc' depth='1'>
{
"name" : "<cms:show k_folder_title />",
"tagLine" : "<cms:show k_folder_desc />",
"cities" :
[
<!-- list cities of the country -->
<cms:set my_city_count='0' />
<cms:if k_folder_immediate_children >
<cms:folders masterpage='malls.php' order='desc' childof=k_folder_name depth='1'>
{
"name" : "<cms:show k_folder_title />",
"suburbs" :
[
<!-- list suburbs of the city -->
<cms:set my_suburb_count='0' />
<cms:if k_folder_immediate_children >
<cms:folders masterpage='malls.php' order='desc' childof=k_folder_name depth='1'>
{
"name" : "<cms:show k_folder_title />"
}
<cms:incr my_suburb_count />
<cms:if my_suburb_count!=k_total_folders> , </cms:if>
</cms:folders>
</cms:if>
]
}
<cms:incr my_city_count />
<cms:if my_city_count!=k_total_folders> , </cms:if>
</cms:folders>
</cms:if>
]
}
<cms:incr my_country_count />
<cms:if my_country_count!=k_total_folders> , </cms:if>
</cms:folders>
]
}
A short description of the code -
Since we have a fixed hierarchy with a maximum depth of 3 levels, I chose to use the cms:folders thrice (one for each level).
Each item in the
JSON array needs to be terminated by a comma (except the lat item). This was handled by using a counter for each level (my_city_count etc.).
Please remove all <!-- --> comments from the code as those are only to make things clear for you.With the code above, the following folder hierarchy -
Singapore
Phillipines
- Pasay City
- Bay City
Australia
- Sidney
-- Paramatta
- Perth
-- Innaloo
-- Canington
-- Booragon
- results in the following
JSON (validated by
http://jsonlint.com/)
- Code: Select all
{
"countries": [
{
"name": "Singapore",
"tagLine": "",
"cities": []
},
{
"name": "Phillipines",
"tagLine": "",
"cities": [
{
"name": "Pasay City",
"suburbs": []
},
{
"name": "Bay City",
"suburbs": []
}
]
},
{
"name": "Australia",
"tagLine": "",
"cities": [
{
"name": "Sidney",
"suburbs": [
{
"name": "Paramatta"
}
]
},
{
"name": "Perth",
"suburbs": [
{
"name": "Innaloo"
},
{
"name": "Canington"
},
{
"name": "Booragon"
}
]
}
]
}
]
}
Hope this helps.