0

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?

srh snl
  • 797
  • 1
  • 19
  • 42
  • That's JSONP (essentially returns JavaScript), not JSON. They're totally different. Also, if you *were* getting JSON back it's going to be implicitly parsed by jQuery, calling `$.parseJSON()` and passing it something that isn't a JSON string isn't going to work. – Anthony Grist Feb 06 '14 at 09:36
  • 1
    For example: `jsondata[0]['y-data']` – Felix Kling Feb 06 '14 at 09:36
  • possible duplicate of [Access / process (nested) objects, arrays or JSON](http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – Felix Kling Feb 06 '14 at 09:37
  • @Anthony: My thoughts exactly, however, given the following information, it looks like the server is actually returning JSON which is already parsed by jQuery. – Felix Kling Feb 06 '14 at 09:38

1 Answers1

2

you can access json that easy:

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

or directly:

alert(jsondata[0]['y-data']);
Manuel Richarz
  • 1,916
  • 13
  • 27