需要帮助将此对象解析为JSON(javascript)

时间:2022-08-26 11:24:05

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

我试图在javascript中使用JSON.parse方法解析JSON数据(在底部)。以下是代码。

 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.

根据我的上面的代码应该解析JSON数据并存储到myData。但应该给我233307的警报给了我一个不确定的。我觉得我做得很对。有帮助吗?谢谢。

{"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"}

3 个解决方案

#1


1  

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.

    你不需要JSON.parse();通过将json指定为GET请求的dataType,您可以让jQuery自动将响应解析为JSON。因此,当调用display()时,x已经是JSON对象而不是需要解析的字符串。

  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.

    您似乎想要在display()中迭代一个数组,但是示例响应不是一个数组,它只是一个以逗号分隔的JSON对象列表。它不是有效的JSON,除非它被包含在[]中(使其成为一个数组)。如果那确实是你的回复,那么jQuery将无法解析它并且x将是未定义的。

  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:

    最后,(一旦你修复了#2),你已经在遍历数组并且myData得到(重新)分配循环的每次迭代到下一个JSON对象,并最终设置为最后一个JSON对象,这不是一个数组,所以myData [2]无效。它应该只是myData.uid。或者不是遍历数组,因为它们都具有相同的uid,您可以将display()更改为:

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

#2


0  

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"}]

#3


0  

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.

?循环的每次迭代都会覆盖之前的“myData”。我也不知道你要用x变量做什么...它是一个JSON对象,而不是一个数组,它也不包含字符串,因此你无法评估它们。

#1


1  

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.

    你不需要JSON.parse();通过将json指定为GET请求的dataType,您可以让jQuery自动将响应解析为JSON。因此,当调用display()时,x已经是JSON对象而不是需要解析的字符串。

  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.

    您似乎想要在display()中迭代一个数组,但是示例响应不是一个数组,它只是一个以逗号分隔的JSON对象列表。它不是有效的JSON,除非它被包含在[]中(使其成为一个数组)。如果那确实是你的回复,那么jQuery将无法解析它并且x将是未定义的。

  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:

    最后,(一旦你修复了#2),你已经在遍历数组并且myData得到(重新)分配循环的每次迭代到下一个JSON对象,并最终设置为最后一个JSON对象,这不是一个数组,所以myData [2]无效。它应该只是myData.uid。或者不是遍历数组,因为它们都具有相同的uid,您可以将display()更改为:

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

#2


0  

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"}]

#3


0  

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.

?循环的每次迭代都会覆盖之前的“myData”。我也不知道你要用x变量做什么...它是一个JSON对象,而不是一个数组,它也不包含字符串,因此你无法评估它们。