2 adalli2008 adalli2008 于 2014.02.02 08:54 提问

jquery函数 读取 json 文件后的运用

我试图通过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) {

<tr id="paper-<?php echo $method['contentID'];?>">
        <tr>
      <td style="background-color:#FFFF00;">&nbsp;</td>
      <td colspan="1" rowspan="5" name="paper" style="padding:5px;background-color:white">
<?php echo "<div class=\"title\"><div class=\"presentation-title\" ><span class=\"paper-title\"><a  id=\"title-".$method['contentID']."\" href=\"presentation2.php?conferenceID=".$method['conferenceID']."&presentationID=".$method['presentationID']."\">".$method['title']."</a></span>"; ?>

请问怎么样能够把函数读到的json文件用变量$method保存,这样所有json内的参数我就可以用method.XX的方式直接取到值。感谢大神赐教!

2个回答

lt318025
lt318025   2014.03.04 09:51

ajax我能想到的就是这个

qingzhishuishou
qingzhishuishou   2014.03.18 17:31

ajax,调用的时候直接data[i].字段

Csdn user default icon
上传中...
上传图片
插入图片