我已经使用了一段时间了,我似乎无法弄明白 我发布了一个问题,它有几个很好的答案我正在使用第二个答案的代码(你应该阅读我的第一篇文章,以了解我来自哪里)parsing nested JSON String objects with JQuery/PHP?
所以基于代码User:Useless Code
它的工作完美,但问题是数据包含在同一页面文件中,并且只有一次专辑...我需要从中拉出json
http://www.ggcc.tv/json2php/posts.php
并让主索引页面ggcc.tv/json2php/index.html列出post.php中的所有相册和内容
请帮助并使用完整代码(因为我是json / php / jquery等新手......
根据用户建议使用代码:无用代码
基本上我需要代码(用户建议:无用代码)从http://www.ggcc.tv/json2php/posts.php中提取数据/每张专辑并列出每张专辑及其曲目
请阅读此帖子我的原帖并在此留下您的答案,谢谢
答案 0 :(得分:0)
试试这个:
<script type="text/javascript">
$(document).ready(function() {
var url="/json2php/posts.php";
$.getJSON(url,function(data){
$.each(data.posts,function(i,post) {
var content,
trackInfo = '',
tracks = post.tracks;
// loop over the tracks and collect info
$.each(tracks, function (i) {
trackInfo += '<a href="' + tracks[i].url + '">' + tracks[i].name + '</a> ';
});
content = '<div class="post">'+
'<h1>'+post.album+'</h1>'+
'<p><img src="'+post.artwork+'"width="250"></img></p>'+
'<p><strong>'+post.church+'</strong></p>'+
'<p>Description: <strong>'+post.des+'</strong></p>'+
'<p>Base url: <em>'+post.baseurl+'</em></p>'+
'<p>Tracks: <li>'+ trackInfo +'</li></p>';
'<hr>'+
'</div>'
$("#content").append(content);
});
});
});
</script>
此外,您在与此列表页面相同的域上连接的posts.php是什么?我认为它是,但如果不是那么你应该知道浏览器将阻止跨域通信,但服务器不会这样你可以通过在webroot / json2php / posts.php创建一个文件来解决这个问题:
<?php
echo file_get_contents('http://www.ggcc.tv/json2php/posts.php');
?>
答案 1 :(得分:0)
远离尝试自己解析json ..使用专门将json转换为html的jquery模板引擎,如json2html。
下面是一个完整的代码示例(确定您要显示的一些相册详细信息,应该很容易添加剩下的内容),这会占用您的一个相册并呈现曲目列表。
请注意,嵌套轨道使用对json2html
的子调用进行渲染{tag:'div',children:function(obj){return($.json2html(obj.tracks,track_transform));}}
使用变换'track_transform'将轨道渲染成链接。
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="http://json2html.com/js/jquery.json2html-2.3-min.js"></script>
<div id='list'></div>
<script>
//JSON that you wanted to render from your earlier post
var json =
[{
"id":"All Things Are Possible",
"key":"All Things Are Possible",
"doc":"All Things Are Possible",
"album":"All Things Are Possible",
"artwork":"http://godsgypsychristianchurch.net/music_artwork/DEFAULT_COVER2.png",
"baseurl":"http://www.godsgypsychristianchurch.net/music",
"church":"Atlanta GA",
"des":"All Things Are Possible from the Atlanta GA Church, Pastor Nick White",
"tracks":[
{"name":"1 Intro",
"url":"/Atlanta%20GA/All%20things%20are%20possible/01%20Intro.mp3"},
{"name":"2 Wo si O Drom",
"url":"/Atlanta%20GA/All%20things%20are%20possible/02%20Wo%20si%20O%20drom.mp3"},
{"name":"3 Nas Murrgo Shov",
"url":"/Atlanta%20GA/All%20things%20are%20possible/03%20Nas%20murrgo%20shov.mp3"},
{"name":"4 To Cho Vos",
"url":"/Atlanta%20GA/All%20things%20are%20possible/04%20To%20cho%20vos.mp3"},
{"name":"5 Tu Son Kai Sastridas",
"url":"/Atlanta%20GA/All%20things%20are%20possible/05%20Tu%20son%20kai%20sastridas.mp3"},
{"name":"6 Now I Am Strong",
"url":"/Atlanta%20GA/All%20things%20are%20possible/06%20Now%20I%20am%20strong.mp3"},
{"name":"7 Sorr Nevee",
"url":"/Atlanta%20GA/All%20things%20are%20possible/07%20Zorr%20nevee.mp3"},
{"name":"8 Preaching",
"url":"/Atlanta%20GA/All%20things%20are%20possible/08%20Preaching.mp3"},
{"name":"9 Arkadyan Amey",
"url":"/Atlanta%20GA/All%20things%20are%20possible/09%20Arkadyan%20amey.mp3"},
{"name":"10 O Christo Ka Wudarr",
"url":"/Atlanta%20GA/All%20things%20are%20possible/10%20O%20Christo%20ka%20wudarr.mp3"},
{"name":"11 Eloi, Eloi",
"url":"/Atlanta%20GA/All%20things%20are%20possible/11%20Eloi%2C%20Eloi.mp3"},
{"name":"12 Amadow Dell",
"url":"/Atlanta%20GA/All%20things%20are%20possible/12%20Amadow%20Dell.mp3"},
{"name":"13 Sastiar Amey Devla",
"url":"/Atlanta%20GA/All%20things%20are%20possible/13%20Sastiar%20amey%20Devla.mp3"},
{"name":"14 Tu Skepeese",
"url":"/Atlanta%20GA/All%20things%20are%20possible/14%20Tu%20skepeese.mp3"},
{"name":"15 Dov Ma Godgee",
"url":"/Atlanta%20GA/All%20things%20are%20possible/15%20Dov%20ma%20godgee.mp3"},
{"name":"16 The Lord is my strength",
"url":"/Atlanta%20GA/All%20things%20are%20possible/16%20The%20Lors%20is%20my%20strength.mp3"}
]
}];
//Transform used to create a link for the track
var track_transform = {tag:'a',html:'.name',href:'.url'};
//Transform used to create an album
var album_transform =
{tag:'div',class:'post',children:[
{tag:'h1',html:'.album'},
{tag:'img',src:'.artwork'},
{tag:'p',children:[
{tag:'span',html:'Tracks: '},
{tag:'div',children:function(obj){return($.json2html(obj.tracks,track_transform));}}
]}
]};
//Render the json into a list of albums
$('#list').json2html(json, album_transform);
</script>