0

这就是我问题的本质:有必要列出所有“文件”:

{
    "results": [{
        "fromuserid": "Anonymous",
        "touserid": "sd68Kbmc02",
        "file": "943easd709bfb2f6",
        "subject": "test",
        "message": "ddd",
        "createdAt": "2013-07-18T20:16:08.023Z",
        "updatedAt": "2013-07-18T20:16:08.023Z",
        "objectId": "bRDvHb4X4M"
    }, {
        "fromuserid": "Anonymous",
        "touserid": "sd68Kbmc02",
        "file": "ef763asd134a8125",
        "subject": "test",
        "message": "ddd",
        "createdAt": "2013-07-18T20:13:56.997Z",
        "updatedAt": "2013-07-18T20:13:56.997Z",
        "objectId": "GaLWnbSFtg"
    }, {
        "fromuserid": "Anonymous",
        "touserid": "sd68Kbmc02",
        "file": "5e7ae0sd5f1b48d0",
        "subject": "etesrtes",
        "message": "dfv fv f",
        "createdAt": "2013-07-18T16:09:20.403Z",
        "updatedAt": "2013-07-18T16:09:20.403Z",
        "objectId": "X83Qd7ctwi"
    }]
}

我用:

$.getJSON("http://domain.me/user/show_user/name/?callback=?", function(data) {
    $('#tile').html("<a href='http://domain.me/?img=" + data['results'][0]['file'] + "' target='_blank'><img src='http://domain.me/" + data['results'][0]['file'] + ".jpg'/></a>");
});

我得到一条线。您想显示所有行data['results'][0]['file']

4

2 回答 2

1
$.getJSON("http://domain.me/user/show_user/name/?callback=?",
    function(data) 
        {
          var results = [];
          $.each(data['results'], function(i, result) {
            results.push("<a href='http://domain.me/?img=" + result['file'] + "' target='_blank'><img src='http://domain.me/" + result['file'] + ".jpg'/></a>");
          });
          $('#tile').html(results.join(""));
        }
);
于 2013-07-18T20:36:37.400 回答
0

您必须解析并遍历解析的 JSON 数据的元素,这很容易做到Array.map()(它在所有浏览器中都受支持,但 IE<9):

$.getJSON("http://domain.me/user/show_user/name/?callback=?", function(data) {
    var html = data.results.map(function(item, index, array) {
        return array[index].file;
    });
    $('#tile').html(html.join(", "));
});
于 2013-07-18T20:43:02.463 回答