【发布时间】:2011-11-28 12:26:46
【问题描述】:
我刚刚完成了 Sencha Touch 教程,效果很好。我目前正在使用 PHP 将静态 JSON 数据文件(本教程使用)转换为从 MySQL 数据库动态生成的数据 (JSON) 文件。本教程中使用的 JSON 文件(示例文件的链接位于下方)具有复杂的结构,我在将我的 SQL 结果转换为教程中使用的确切 JSON 格式时遇到问题。下面列出了源代码的链接。重点放在 JSON 文件上(第三个链接)。如果我可以简单地复制这个文件,那么项目的其余部分就对我有用。
煎茶触控教程位于: http://www.sencha.com/learn/intro-to-the-nested-list-component/
该项目的所有代码都可以位于(我只允许发布两个链接,希望您能解释下面的链接): github dot com / nelstrom / Sencha-Touch-nested-list-demo
静态 JSON 数据文件的示例可以查看:https://github.com/senchalearn/Nested-list-demo/blob/master/data/albums.json
我有一个数据库和 sql,它以以下结构输出数据:
Genre Artist Album Track Duration
ROCK MUSE Absolution Intro 0:23
ROCK MUSE Absolution Apolcalypse Please 4:13
ROCK MUSE The Resistance Uprising 5:03
ROCK SEVENDUST Next Hero 3:48
FUNK PRIMUS Antipop The Antipop 5:33
FUNK PRIMUS Antipop Ballad of Bodacious 2:29
我有以下输出 JSON 但格式不正确的 php,不幸的是,它已经尽可能接近了 - 抱歉,我的 PHP 有点平均:)
$result = mysql_query($query,$link) or die('Errant query: '.$query);
$model = array();
if(mysql_num_rows($result)) {
while($e = mysql_fetch_assoc($result)) {
$model['Genre'] = $e['Genre'];
$model['Artist'] = $e['Artist'];
$model['Album'] = $e['Album'];
$model['items'][] = array(
'text' => $e['Track'],
'duration' => $e['Duration']
);
}
}
header('Content-type: application/json');
echo json_encode(array('items'=>$model));}
以下是上面 PHP 代码输出的 JSON 示例:
{
"items": {
"Genre": "ROCK",
"Artist": "MUSE",
"Album": "Absolution",
"items": [
{
"text": "Intro",
"duration": "0:23"
},
{
"text": "Apolcalypse Please",
"duration": "4:13"
}
]
}
}
很遗憾,这种 JSON 格式不正确。主要问题是循环并在正确的位置应用方括号'['']'。我在下面包含了一个简短的示例:
{
"items": [
{
"model": "Genre",
"items": [
{
"model": "Artist",
"items": [
{
"model": "Album",
"items": [
{
"model": "Track",
"duration": 96,
"text": "Introduction",
"items": [
],
"info": "",
"leaf": true
},
{
"model": "Track",
"duration": 155,
"text": "Please Accept My Love",
"items": [
],
"info": "",
"leaf": true
}
],
"text": "Live in Cook County Jail",
"info": "<p>Live in Cook County Jail is a 1971 live album by B.B. King recorded in Cook County Jail, Chicago, Illinois. It was ranked as number 499 in the book version of Rolling Stone's 500 Greatest Albums of All Time.</p>",
"leaf": true
}
],
"text": "B.B.King",
"info": "<p>Riley B. King aka B. B. King (born September 16th, 1925 in Itta Bena, Mississippi) is a well known American blues guitarist and songwriter. He is among the most respected electric guitarists. </p><p>One of King’s trademarks is naming his guitar (Gibson ES335) “Lucilleâ€. In the 1950s in a bar in Twist, Arkansas two men got into a fight, accidentally knocking over a bucket of burning kerosene (used for heating) and setting the establishment on fire. Risking his life, B.B. King ran back into the collapsing building to retrieve his guitar.</p>",
"leaf": false
},
],
"text": "Blues",
"info": "",
"leaf": false
}
]
}
提前感谢您查看此内容,如果内容冗长,我很抱歉,但我只是想确保我已包含所有内容。如果您需要更多信息,请告诉我。
亲切的问候
【问题讨论】:
-
只是为了让您理解,您正在使用一个关系数据库并将其一直压缩为一个对象。在实践中,很少这样做(除非它是出于存档目的,我只是自己拍摄数据库的快照......)。相反,使用各种
SELECT查询“过滤”数据子集,以挑选出所需的特定信息。我了解您正在尝试模仿本教程,但您似乎才刚刚开始使用数据库等一般情况,我想在您尝试对每个数据库调用执行类似操作之前,我会告诉您。
标签: php mysql json sencha-touch