سؤال

Im using $.getJSON of jquery to get the data that I need from the server. Here's how the it looks like:

$.getJSON("/dataParser/parseVoltage",function(jsondata, status)
{
    if (status == "error") {
    console.log("Error encountered while getting the data from server.");
    }
    else if (status == "success") 
    {
        alert(jsondata.constructor.name + " array : " + $.parseJSON(jsondata));
        console.log(jsondata);
        for(i= 0; i< jsondata.length; i++)
        {
            console.log(jsondata[i]);
        }               
    }
});

The json string that will be coming from the server is generated by C. So basically, the strng looks like this..

render("[{\"y-data\":0, \"x-data\":2.513},{\"y-data\":1, \"x-data\":3.038},{\"y-data\":2, \"x-data\":12.625}]");

Since Im assuming that I will be getting a json formatted file, I tried this

$.parseJSON(jsondata); but the output is null

I also tried this:

jsondata.constructor.name the output is Array

when I tried to log the data, this is the result

console.log(jsondata) .. output is [object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

when I tried to print all the data using for loop this is the result

for(i= 0; i< jsondata.length; i++)
{
alert("arraydata" + i + " " + jsondata[i]);
}

the result is 
arraydata0 [object Object]
arraydata1 [object Object]
arraydata2 [object Object]
arraydata3 [object Object]
...

when I check what exactly the response of server, this is the result..

[{"y-data":0, "x-data":2.513},{"y-data":1, "x-data":3.038},{"y-data":2, "x-data":12.625},{"y-data":3, "x-data":1.24},{"y-data":4, "x-data":1.013},{"y-data":5, "x-data":3.317}]

Now, how do I access/ read this kind of data?

هل كانت مفيدة؟

المحلول

you can access json that easy:

jQuery.each(jsondata, function() {
  alert(this['y-data'] + ' - ' + this['x-data']);
});

or directly:

alert(jsondata[0]['y-data']);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top