0

I am making a listview using JSON and are successfull with this, but... I was wandering if it was possible to send other data to my page, which is "not in an array" with the same JSON call without having to put them through the $each?

Here is my JSON: ---EDIT---

[{"menutime":"2013-04-23 19:23:42"},{"list":"menu1"},{"list":"Menu2"},{"list":"Menu3"}]

As you can see, I have 3 "list" which I get into an listview like this:

dataCallback = function (data) {
    var content = [];
    // var now = data.menutime; // HERE I WANT THE MENUTIME TO BE PUT INTO A VARIABLE //
    $.each(data, function (i, val) {
        content.push(val.list); // GETTING ALL THE MENUS //
    });
    $('#games').html(content.join('')).listview('refresh');
}

I don't get the menutime... How do I do this?

Thanks in advance :-)

---------------- EDIT ----------------

I have tried this like Kevin B has suggested but I cant get it to work?!

dataCallback = function(data){
    alert("Date: "+data.menutime);

    //var content = data.list.join("");
    //var now = data.menutime; // HERE I WANT THE MENUTIME TO BE PUT INTO A VARIABLE //

    $('#userbar').html(data.profile); // FETCHING USERS DATA FOR PROFILEBAR //
    //$('#games').html(content).listview('refresh');
}

In my alert I get "Undefined".

My "jason" looks like this:

([{"menutime":"2013-04-25 15:57:50"},{"profile":"troels"}])

Any ideas why this dont work?

4

3 回答 3

2

如果您可以修改服务器返回的内容,请使其返回结构更适合其包含的内容的数据。

{ 
    "menutime": "2013-04-23 19:23:42",
    "list": ["menu1","menu2","menu3"]
}

现在您的回调可以更简单:

dataCallback = function (data) {
    var content = data.list.join("");
    var now = data.menutime; // HERE I WANT THE MENUTIME TO BE PUT INTO A VARIABLE //
    $('#games').html(content).listview('refresh');
}
于 2013-04-23T19:33:19.633 回答
0

您没有访问 menutime -

content.push(val.menutime);

你的 json 应该是这样的——

 [{"menutime":"2013-04-23 19:23:42"},{"list":"menu1"},{"list":"Menu2"},{"list":"Menu3"}]
于 2013-04-23T19:31:47.907 回答
0

您可以将数组包装在一个对象中

dataCallback = function (data) {
    var content = [];
    // var now = data.menutime; // HERE I WANT THE MENUTIME TO BE PUT INTO A VARIABLE //
    $.each(data, function (i, val) {
        content.push(val.list); // GETTING ALL THE MENUS //
    });
    var sendData = {
          content :content ,
          menutime:now
    };
    $('#games').html(sendData .content.join('')).listview('refresh');
    ///use sendData.menutime to get menutime
} 
于 2013-04-23T19:32:38.243 回答