我試圖通過jquery讀取一份json檔案,然後把他放到php中進行動態布局並輸出。現在遇到一個問題,就是我不知道如何將函數讀取到的json中的參數值儲存下來,然後用php讀到這些值。希望有哪位朋友知道可以給與協助(最好有代碼)。
首先,json檔案格式如下:
[
{ "counter": "0", "contentID": "5876", "score": "1.20501602970259", "presentationID": "3496", "conferenceID": "85", "title": "Personalized Network Updates: Increasing Social Interactions and Contributions in Social Networks", "acmlink": "null", "DOI": "http://www.springerlink.com/content/872x206h570ln625/fulltext.pdf", "contentType": "Long Research Paper", "contentTrack": "1", "authors": [ { "authorID": "160", "name": "Shlomo Berkovsky" } , { "authorID": "155", "name": "Jill Freyne" } , { "authorID": "2947", "name": "Gregory Smith" } ], "tags": ["3545", "contribution", "evaluation", "motivation", "news feed", "personalisation", "personalization", "personalized news feeds", "ranking", "recommender", "recsys", "shlomo berkovsky", "sna", "social network", "social networks", "social-network"], "methodID": [0, 102, 101]},{ "counter": "1", "contentID": "5883", "score": "0.984497667264824", "presentationID": "3503", "conferenceID": "85", "title": "Users and Noise: Estimating the Magic Barrier of Recommender Systems", "acmlink": "null", "DOI": "http://www.springerlink.com/content/g70242127h5kj186/fulltext.pdf", "contentType": "Long Research Paper", "contentTrack": "1", "authors": [ { "authorID": "3120", "name": "Alan Said" }
]
我用jquery讀取json檔案的代碼:
$(function()
{
$("#btn").click(function()
{
$.getJSON("communityRecSys.json",function (data)
{
var $jsontip = $("#jsonTip");
var strHtml = " ";//儲存變數
$jsontip.empty();//清空內容
$.each(data,function (infoIndex,info)
{
strHtml += "title:"+info["title"]+"
";
strHtml += "Authors:"+info["authors"]+"
";
strHtml += "Types:"+info["type"]+"
";
strHtml += "Track:"+info["contentTrack"]+"
";
strHtml += "DOI:"+info["DOI"]+"
";
strHtml += "
"
//這裡只取了幾個需要的值
});
});
});
});
然後在PHP中用這段代碼測試,值能夠正常顯示:
說明值已經正常被讀入。
下面是我希望進行php布局的代碼。(proceedings是對每個模組的命名,method是我想用來代表json檔案自己設的變數名稱)
foreach( $proceedings as $method) {
".$method['title'].""; ?>
請問怎麼樣能夠把函數讀到的json檔案用變數$method儲存,這樣所有json內的參數我就可以用method.XX的方式直接取到值。感謝大神賜教!