开发者

accessing json data from jquery

I'm creating an ajax app using jQuery 1.4.2 and I've tried using using get(), post() a开发者_运维技巧nd the ajax() method itself. My php service returns:

[{"k":"label0","v":0.5},{"k":"label1","v":99.43},{"k":"label2","v":2.46},{"k":"label3","v":46.29},{"status":"OK"}]

in my success callback I have tried accessing as json.status and json[0][0] but it always returns "undefined". what am I doing wrong?

function getSysinfo(source) {
    var json = null;
    $.ajax({
        url: source,
        type: 'POST',
        dataType: 'json',
        success: function (data) {
            json = eval("(" + data + ")");
            $('#data').html(json.status);
            alert(json[0][0]);
            refreshChart(json);
        },
        error: function (request, status, error) {
            alert("REQUEST:\t" + request + "\nSTATUS:\t" + status + 
                  "\nERROR:\t" + error);
        }
    });
    return json;
}

I've been googling this for days. How the heck do I access the returned data? any help would be appreciated.


To access that status value you would need:

data[4].status

This is because it is an object stored in the the fifth element in an array, with status being a property on the object.


Your JSON-data looks like this:

[
    {
        "k": "label0",
        "v": 0.5
    },
    {
        "k": "label1",
        "v": 99.43
    },
    {
        "k": "label2",
        "v": 2.46
    },
    {
        "k": "label3",
        "v": 46.29
    },
    {
        "status": "OK"
    }
]

You would have to read your status using

json[4].status

with the 4 as a magical number or length-1 - not desirable. I would consider modifying your servers response to something more useful like this:

{
    "status": "OK",
    "entries": [ ... ] // add your data here
}


In your success callback try:

var parsed = $.parseJSON(data);
$.each(parsed, function (i, jsondata) {
    alert( jsondata.k );
    alert( jsondata.v );
});


You don't need the eval("("+data+")");. jQuery is automatically parsing the JSON response for you because you specified dataType:'json'

From the jQuery docs for dataType:

"json": Evaluates the response as JSON and returns a JavaScript object. In jQuery 1.4 the JSON data is parsed in a strict manner; any malformed JSON is rejected and a parse error is thrown. (See json.org for more information on proper JSON formatting.)


no need to use eval any more use below code which can be more for json

$.getJSON(url+query,function(json){
            $.each(json,function(i,value){

            });
        });


nategood already wrote that you don't need do do anything with data, it's already an object.

In this case it's an array, if you like to access the status, you need to retrieve it from the last item of the data-array(that's where you'll find it in this array):

  data[data.length-1].status

But maybe you should think about another structure of your JSON, it doesn't look very comfortable.

Something like that:

{
 "items":[
         {"k":"label0","v":0.5},
         {"k":"label1","v":99.43},
         {"k":"label2","v":2.46},
         {"k":"label3","v":46.29}
        ],
 "status":"OK"
}

...should be easier to handle, because you can simply access data.status instead of first looking where you may find it inside the response(what may be error-prone ).


The data parameter is the decoded JSON as you can see in this example:

http://www.jsfiddle.net/rLprV/1/


$.ajax({
        url: url,
        type: 'POST',
        dataType: 'json',
        data: formData,
        showLoader:true,
        success: function (response) {
            var parsed =  JSON.parse(JSON.stringify(response));
            $.each(parsed, function (key, val) {
                alert( val.name );                      
            });
        },
        error: function (err) {
            alert("Please enter a valid id")    
        }
     });
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜