开发者

jQuery get JSON

Ok so I am able to return the JSON file fine, But what I don't know if how get the right colum (sorry if that make no sence)

Here is the JSON output

[{"total":"85.91"}]

I want to be able to get the word total

As what plain was and is to have one $.ajax function doing all the posting back and forth to the server.

I have ne开发者_如何转开发ver done it this way but hope it can be done.

Here is my jQuery code

    function fetchpage(e,formstring)
{
    $.ajax({
      type: 'POST',
      url: 'system/classes/core.php',
      data: formstring,
      dataType: 'json',
      success: function(data){
          $.each(data,function(i,myinfo){
            alert(data[0]);     
          });
      },
      error: function(data){
      $.each(data,function(i,myinfo){
            alert(i);       
          });
      }

    });
}


I'm assuming you want to retrieve the object keys rather than accessing each directly, eg obj.total.

Try the following...

success: function(data) {
    for (var i = 0; i < data.length; i++) {
        var obj = data[i];
        for (var key in obj) {
            if (obj.hasOwnProperty(key)) {
                // see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/hasOwnProperty
                console.log(key, obj[key]);
            }
        }
    }
});


success: function(data) {
    $.each(data, function(i, myinfo) {
        alert(myinfo.total);     
    });
});


[EDIT] See Phil's answer for double parsing (JSON + Array)


You can use the .getJSON()doc API that will parse your returned data:

$.getJSON('system/classes/core.php', function(data) {    
  $.each(data, function(key, val) {
    alert("my key" + key);
    alert("my val" + val);
  });
});


hi you can get the word total by this way

$.each(data, function(key, val) { alert(key) })

this will give you word total

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜