k2_content模块输出json数据格式

k2_content模块是k2的一个自带的强大的内容输出模块,它不仅可以按你的要求输出文章列表,还可以输出json格式的数据。

此功能需要使用k2 2.6.x或以上的版本。

输出Json数据的方法:

在对应的k2_content模块的模板文件找到以下代码:

<?php echo JRoute::_('index.php?option=com_k2&view=itemlist&format=feed&moduleID='.$module->id); ?>

这是模块的rss订阅链接地址,将这地址中的format=feed改成format=json就可以,即

<?php echo  JRoute::_('index.php?option=com_k2&view=itemlist&format=feed&moduleID='.$module->id);  ?>

以上即为json格式的数据源地址。

k2_content模块的json数据格式示例:

{
"site": {
"url": "http://test.ycway.com",
"name": "test site"
},
"items": [
{
"id": "4",
"title": "Building 4",
"alias": "building-4",
"link": "/buildings/building-4.html",
"catid": "1",
"introtext": "<p>Building 4 content</p>",
"fulltext": "",
"extra_fields": [
{
"id": "1",
"name": "address",
"value": "New York",
"type": "radio",
"group": "1",
"published": "1",
"ordering": "1",
"alias": "address"
},
{
"id": "2",
"name": "ares",
"value": "120",
"type": "textfield",
"group": "1",
"published": "1",
"ordering": "2",
"alias": "ares"
},
{
"id": "3",
"name": "AA",
"value": "AA1",
"type": "select",
"group": "1",
"published": "1",
"ordering": "3",
"alias": "AA"
},
{
"id": "4",
"name": "AA1 XX",
"value": "BB1",
"type": "select",
"group": "1",
"published": "1",
"ordering": "4",
"alias": "AAXX"
},
{
"id": "5",
"name": "AA2 XX",
"value": "CC1",
"type": "select",
"group": "1",
"published": "1",
"ordering": "5",
"alias": "AAXX"
},
{
"id": "6",
"name": "AA3 XX",
"value": "DD1",
"type": "select",
"group": "1",
"published": "1",
"ordering": "6",
"alias": "AAXX"
},
{
"id": "7",
"name": "Boroughs",
"value": "Kings County",
"type": "radio",
"group": "1",
"published": "1",
"ordering": "7",
"alias": "Boroughs"
}
],
"created": "2012-11-13 03:52:50",
"created_by_alias": "",
"modified": "2012-12-03 14:38:57",
"featured": "0",
"image": "",
"imageWidth": "",
"image_caption": "",
"image_credits": "",
"imageXSmall": "",
"imageSmall": "",
"imageMedium": "",
"imageLarge": "",
"imageXLarge": "",
"video": "",
"video_caption": "",
"video_credits": "",
"gallery": null,
"hits": "7",
"category": {
"id": "1",
"name": "buildings",
"alias": "buildings",
"link": "/buildings.html",
"description": "",
"image": "",
"ordering": "1"
},
"tags": [
{
"id": "1",
"name": "tag1",
"published": "1",
"link": "/tag/tag1.html"
},
{
"id": "2",
"name": "tag2",
"published": "1",
"link": "/tag/tag2.html"
},
{
"id": "3",
"name": "tag3",
"published": "1",
"link": "/tag/tag3.html"
}
],
"attachments": [],
"votingPercentage": 0,
"numOfvotes": "(0 votes)",
"author": {
"name": "Administrator",
"link": "/blog/Administrator.html",
"avatar": "http://www.gravatar.com/avatar/cc8caa103a74a6557490f16f0c9cb84a?s=100&default=http%3A%2F%2Ftest.ycway.com%2Fcomponents%2Fcom_k2%2Fimages%2Fplaceholder%2Fuser.png",
"profile": {
"_errors": [],
"gender": null,
"url": ""
}
},
"numOfComments": "0",
"events": {
"BeforeDisplay": "",
"AfterDisplay": "",
"AfterDisplayTitle": "",
"BeforeDisplayContent": "",
"AfterDisplayContent": "",
"K2BeforeDisplay": "",
"K2AfterDisplay": "",
"K2AfterDisplayTitle": "",
"K2BeforeDisplayContent": "",
"K2AfterDisplayContent": "",
"K2CommentsCounter": ""
},
"language": ""
},
{
"id": "3",
"title": "Building 3",
"alias": "building-3",
"link": "/buildings/building-3.html",
"catid": "1",
"introtext": "<p>Building 3 content</p>",
"fulltext": "",
"extra_fields": [
{
"id": "1",
"name": "address",
"value": "Hong Kong",
"type": "radio",
"group": "1",
"published": "1",
"ordering": "1",
"alias": "address"
},
{
"id": "2",
"name": "ares",
"value": "90",
"type": "textfield",
"group": "1",
"published": "1",
"ordering": "2",
"alias": "ares"
},
{
"id": "3",
"name": "AA",
"value": "AA1",
"type": "select",
"group": "1",
"published": "1",
"ordering": "3",
"alias": "AA"
},
{
"id": "4",
"name": "AA1 XX",
"value": "BB1",
"type": "select",
"group": "1",
"published": "1",
"ordering": "4",
"alias": "AAXX"
},
{
"id": "5",
"name": "AA2 XX",
"value": "CC1",
"type": "select",
"group": "1",
"published": "1",
"ordering": "5",
"alias": "AAXX"
},
{
"id": "6",
"name": "AA3 XX",
"value": "DD1",
"type": "select",
"group": "1",
"published": "1",
"ordering": "6",
"alias": "AAXX"
},
{
"id": "7",
"name": "Boroughs",
"value": "Kowloon City",
"type": "radio",
"group": "1",
"published": "1",
"ordering": "7",
"alias": "Boroughs"
}
],
"created": "2012-11-13 03:52:11",
"created_by_alias": "",
"modified": "2012-12-03 14:39:12",
"featured": "0",
"image": "",
"imageWidth": "",
"image_caption": "",
"image_credits": "",
"imageXSmall": "",
"imageSmall": "",
"imageMedium": "",
"imageLarge": "",
"imageXLarge": "",
"video": "",
"video_caption": "",
"video_credits": "",
"gallery": null,
"hits": "7",
"category": {
"id": "1",
"name": "buildings",
"alias": "buildings",
"link": "/buildings.html",
"description": "",
"image": "",
"ordering": "1"
},
"tags": [
{
"id": "5",
"name": "tag5",
"published": "1",
"link": "/tag/tag5.html"
},
{
"id": "1",
"name": "tag1",
"published": "1",
"link": "/tag/tag1.html"
}
],
"attachments": [],
"votingPercentage": 0,
"numOfvotes": "(0 votes)",
"author": {
"name": "Administrator",
"link": "/blog/Administrator.html",
"avatar": "http://www.gravatar.com/avatar/cc8caa103a74a6557490f16f0c9cb84a?s=100&default=http%3A%2F%2Ftest.ycway.com%2Fcomponents%2Fcom_k2%2Fimages%2Fplaceholder%2Fuser.png",
"profile": {
"_errors": [],
"gender": null,
"url": ""
}
},
"numOfComments": "0",
"events": {
"BeforeDisplay": "",
"AfterDisplay": "",
"AfterDisplayTitle": "",
"BeforeDisplayContent": "",
"AfterDisplayContent": "",
"K2BeforeDisplay": "",
"K2AfterDisplay": "",
"K2AfterDisplayTitle": "",
"K2BeforeDisplayContent": "",
"K2AfterDisplayContent": "",
"K2CommentsCounter": ""
},
"language": ""
},
{
"id": "2",
"title": "Building 2",
"alias": "building-2",
"link": "/buildings/building-2.html",
"catid": "1",
"introtext": "<p>Building 2 content</p>",
"fulltext": "",
"extra_fields": [
{
"id": "1",
"name": "address",
"value": "Paris",
"type": "radio",
"group": "1",
"published": "1",
"ordering": "1",
"alias": "address"
},
{
"id": "2",
"name": "ares",
"value": "150",
"type": "textfield",
"group": "1",
"published": "1",
"ordering": "2",
"alias": "ares"
},
{
"id": "3",
"name": "AA",
"value": "AA1",
"type": "select",
"group": "1",
"published": "1",
"ordering": "3",
"alias": "AA"
},
{
"id": "4",
"name": "AA1 XX",
"value": "BB1",
"type": "select",
"group": "1",
"published": "1",
"ordering": "4",
"alias": "AAXX"
},
{
"id": "5",
"name": "AA2 XX",
"value": "CC1",
"type": "select",
"group": "1",
"published": "1",
"ordering": "5",
"alias": "AAXX"
},
{
"id": "6",
"name": "AA3 XX",
"value": "DD1",
"type": "select",
"group": "1",
"published": "1",
"ordering": "6",
"alias": "AAXX"
},
{
"id": "7",
"name": "Boroughs",
"value": "Hauts de Seine",
"type": "radio",
"group": "1",
"published": "1",
"ordering": "7",
"alias": "Boroughs"
}
],
"created": "2012-11-13 03:51:17",
"created_by_alias": "",
"modified": "2012-12-03 14:39:26",
"featured": "0",
"image": "",
"imageWidth": "",
"image_caption": "",
"image_credits": "",
"imageXSmall": "",
"imageSmall": "",
"imageMedium": "",
"imageLarge": "",
"imageXLarge": "",
"video": "",
"video_caption": "",
"video_credits": "",
"gallery": null,
"hits": "2",
"category": {
"id": "1",
"name": "buildings",
"alias": "buildings",
"link": "/buildings.html",
"description": "",
"image": "",
"ordering": "1"
},
"tags": [
{
"id": "2",
"name": "tag2",
"published": "1",
"link": "/tag/tag2.html"
},
{
"id": "4",
"name": "tag4",
"published": "1",
"link": "/tag/tag4.html"
}
],
"attachments": [],
"votingPercentage": 0,
"numOfvotes": "(0 votes)",
"author": {
"name": "Administrator",
"link": "/blog/Administrator.html",
"avatar": "http://www.gravatar.com/avatar/cc8caa103a74a6557490f16f0c9cb84a?s=100&default=http%3A%2F%2Ftest.ycway.com%2Fcomponents%2Fcom_k2%2Fimages%2Fplaceholder%2Fuser.png",
"profile": {
"_errors": [],
"gender": null,
"url": ""
}
},
"numOfComments": "0",
"events": {
"BeforeDisplay": "",
"AfterDisplay": "",
"AfterDisplayTitle": "",
"BeforeDisplayContent": "",
"AfterDisplayContent": "",
"K2BeforeDisplay": "",
"K2AfterDisplay": "",
"K2AfterDisplayTitle": "",
"K2BeforeDisplayContent": "",
"K2AfterDisplayContent": "",
"K2CommentsCounter": ""
},
"language": ""
},
{
"id": "1",
"title": "Building 1",
"alias": "building-1",
"link": "/buildings/building-1.html",
"catid": "1",
"introtext": "<p>Building 1 content</p>",
"fulltext": "",
"extra_fields": [
{
"id": "1",
"name": "address",
"value": "Hong Kong",
"type": "radio",
"group": "1",
"published": "1",
"ordering": "1",
"alias": "address"
},
{
"id": "2",
"name": "ares",
"value": "100",
"type": "textfield",
"group": "1",
"published": "1",
"ordering": "2",
"alias": "ares"
},
{
"id": "3",
"name": "AA",
"value": "AA1",
"type": "select",
"group": "1",
"published": "1",
"ordering": "3",
"alias": "AA"
},
{
"id": "4",
"name": "AA1 XX",
"value": "BB1",
"type": "select",
"group": "1",
"published": "1",
"ordering": "4",
"alias": "AAXX"
},
{
"id": "5",
"name": "AA2 XX",
"value": "CC1",
"type": "select",
"group": "1",
"published": "1",
"ordering": "5",
"alias": "AAXX"
},
{
"id": "6",
"name": "AA3 XX",
"value": "DD1",
"type": "select",
"group": "1",
"published": "1",
"ordering": "6",
"alias": "AAXX"
},
{
"id": "7",
"name": "Boroughs",
"value": "Sham Shui Po",
"type": "radio",
"group": "1",
"published": "1",
"ordering": "7",
"alias": "Boroughs"
}
],
"created": "2012-11-13 03:49:52",
"created_by_alias": "",
"modified": "2012-12-03 14:39:48",
"featured": "0",
"image": "",
"imageWidth": "",
"image_caption": "",
"image_credits": "",
"imageXSmall": "",
"imageSmall": "",
"imageMedium": "",
"imageLarge": "",
"imageXLarge": "",
"video": "",
"video_caption": "",
"video_credits": "",
"gallery": null,
"hits": "0",
"category": {
"id": "1",
"name": "buildings",
"alias": "buildings",
"link": "/buildings.html",
"description": "",
"image": "",
"ordering": "1"
},
"tags": [],
"attachments": [],
"votingPercentage": 0,
"numOfvotes": "(0 votes)",
"author": {
"name": "Administrator",
"link": "/blog/Administrator.html",
"avatar": "http://www.gravatar.com/avatar/cc8caa103a74a6557490f16f0c9cb84a?s=100&default=http%3A%2F%2Ftest.ycway.com%2Fcomponents%2Fcom_k2%2Fimages%2Fplaceholder%2Fuser.png",
"profile": {
"_errors": [],
"gender": null,
"url": ""
}
},
"numOfComments": "0",
"events": {
"BeforeDisplay": "",
"AfterDisplay": "",
"AfterDisplayTitle": "",
"BeforeDisplayContent": "",
"AfterDisplayContent": "",
"K2BeforeDisplay": "",
"K2AfterDisplay": "",
"K2AfterDisplayTitle": "",
"K2BeforeDisplayContent": "",
"K2AfterDisplayContent": "",
"K2CommentsCounter": ""
},
"language": ""
}
]
}

可以看到,json源中的文章数据包含了文章的ID、标题、别名、分类信息、字段信息、作者信息、语言代码等非常齐全的内容。方便大家开发时读取调用。