开发者

Need help parsing this object into JSON (javascript)

开发者 https://www.devze.com 2023-03-01 00:27 出处:网络
I am trying to parse the JSON data (at the bottom) using the JSON.parse method in javascript. Below is the code.

I am trying to parse the JSON data (at the bottom) using the JSON.parse method in javascript. Below is the code.

 var i;
 var myData={};
 $(document).ready(function(){

        $.get("http://localhost:4567/get/233307/loc/15000/10", function(data){
                display(data);

            }, "json");
    });
 function display(x){
    for(i = 0; i <= x.length;i++){
        myData = JSON.parse(x[i]);
    }
    alert(myData[2].uid);
 }

The code above, according to me should parse the JSON data and store to myData. But the alert that should have given me 233307 gives me an undefined. I feel like I'm doing everything right. Any help? Thanks.

{"id":64567868968,"uid":233307,"lat":41.418972,"long":-72.8941315,"date":"2010-11-11T16:01:15-05:00"},{"id":64567803255,"uid":
233307,"lat":41.4189505,"long":-72.89411,"date":"2010-11-11T16:00:13-05:00"},{"id":64567803254,"uid":233307,"l开发者_如何学Goat":
41.4189451666667,"long":-72.8940725,"date":"2010-11-11T15:59:11-05:00"},{"id":64567803253,"uid":233307,"lat":
41.4188646666667,"long":-72.8940831666667,"date":"2010-11-11T15:58:08-05:00"},{"id":64567803252,"uid":233307,"lat":
41.4190095,"long":-72.8941905,"date":"2010-11-11T15:57:06-05:00"},{"id":64567700284,"uid":233307,"lat":
41.418972,"long":-72.894169,"date":"2010-11-11T15:56:04-05:00"}


Three issues I see:

  1. You don't need JSON.parse(); by specifying json as the dataType for the GET request, you ask jQuery to automatically parse the response into JSON. So when display() gets called, x is already a JSON object and not a string that needs to be parsed.

  2. You seem to want to iterate over an array in display() but the sample response is not an array, it is simply a comma-separated list of JSON objects. It is NOT valid JSON unless it was enclosed in [] (making it an array). If that is indeed your response, then jQuery will be unable to parse it and x will be undefined.

  3. Finally, (once you fix #2), you're already iterating over the array and myData gets (re)-assigned with each iteration of the loop to the next JSON object and is finally set to the last JSON object, which is not an array so myData[2] is invalid. It should simply be myData.uid. Or instead of iterating over the array, since they all have the same uid, you might instead change display() to:

     function display(x){
        if(x && x.length > 0){
            alert(myData[0].uid);
        }
     }
    


You are missing array brackets around your objects

[{"id":64567868968,"uid":233307,"lat":41.418972,"long":-72.8941315,"date":"2010-11-11T16:01:15-05:00"},{"id":64567803255,"uid":
233307,"lat":41.4189505,"long":-72.89411,"date":"2010-11-11T16:00:13-05:00"},{"id":64567803254,"uid":233307,"lat":
41.4189451666667,"long":-72.8940725,"date":"2010-11-11T15:59:11-05:00"},{"id":64567803253,"uid":233307,"lat":
41.4188646666667,"long":-72.8940831666667,"date":"2010-11-11T15:58:08-05:00"},{"id":64567803252,"uid":233307,"lat":
41.4190095,"long":-72.8941905,"date":"2010-11-11T15:57:06-05:00"},{"id":64567700284,"uid":233307,"lat":
41.418972,"long":-72.894169,"date":"2010-11-11T15:56:04-05:00"}]


Do you mean this:

function display(x){
    var myData = [];
    for(i = 0; i <= x.length; i++){
        myData.push(JSON.parse(x[i]));
    }
    alert(myData[2].uid);
}

? Each iteration of your loop overwrites the previous "myData". I also can't tell what you're trying to do with the x variable... It's a JSON object, not an array, and it doesn't contain strings either so you can't evaluate them.

0

精彩评论

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

关注公众号