Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用Jquery解析以下JSON数据_Javascript_Jquery_Json - Fatal编程技术网

Javascript 如何使用Jquery解析以下JSON数据

Javascript 如何使用Jquery解析以下JSON数据,javascript,jquery,json,Javascript,Jquery,Json,我是JQuery新手。我有来自服务器的JSON响应,我如何解析它 [ { "note": { "created_at": "2012-04-28T09:41:37Z", "updated_at": "2012-04-28T09:41:37Z", "text": "aaaaaaaaaaafdafasd fasfasd dfa sdfasf asdfa fasdfda", "lng

我是JQuery新手。我有来自服务器的JSON响应,我如何解析它

[
    {
        "note": {
            "created_at": "2012-04-28T09:41:37Z",
            "updated_at": "2012-04-28T09:41:37Z",
            "text": "aaaaaaaaaaafdafasd fasfasd dfa sdfasf asdfa fasdfda",
            "lng": 44.5159794497071,
            "id": 7,
            "deleted": false,
            "user_id": 1,
            "note_type": "text",
            "lat": 40.1884140543842
        }
    },
    [ ... more JSON ...]
]

我如何解析这个

您必须将请求的数据类型设置为“json”,并且数据将在成功回调中被解析

你现在需要知道的一切都在播放中

下面是一个非常简单的示例,说明您可以做什么:

$.ajax({
    url: url, // the service URL. Must answer proper JSON
    data: {  // the parameters of the request. This should be adapted to your case
        param1: value1,
        param2: value2
    },
    dataType: "json",
    type: "POST",
    success: function(resultData) {
        // here, resultData is your parsed json
    },
    error: function() {
        // handle error here
    }
});

您必须将请求的数据类型设置为“json”,并且数据将在成功回调中被解析

你现在需要知道的一切都在播放中

下面是一个非常简单的示例,说明您可以做什么:

$.ajax({
    url: url, // the service URL. Must answer proper JSON
    data: {  // the parameters of the request. This should be adapted to your case
        param1: value1,
        param2: value2
    },
    dataType: "json",
    type: "POST",
    success: function(resultData) {
        // here, resultData is your parsed json
    },
    error: function() {
        // handle error here
    }
});

使用此jQuery方法解析JSON对象。


使用此jQuery方法解析JSON对象。

如果服务器输出实际的JSON(问题中的示例有错误),并且它具有正确的内容类型(
application/JSON
,而不是PHP默认的
text/html
),则无需执行任何操作


jQuery将为您解析它(并在成功处理程序中为您提供一个JavaScript对象)。

如果服务器输出实际的JSON(问题中的示例有错误),并且它具有正确的内容类型(
application/JSON
,而不是PHP默认的
text/html
),那么您不需要做任何事情

jQuery将为您解析它(并在成功处理程序中为您提供一个JavaScript对象)。

这不是JSON。您发布的内容看起来像是一个PHP数组,它用括号括起来,试图将其转换为JSON

用于将来验证JSON

现在,要将PHP数组转换为JSON,请使用它并将其发送到带有特定头的浏览器

$array = array( 'test' => 'sure' );
header('Content-type: application/json');
print json_encode($array);
exit;
现在,您将拥有可以在JavaScript中使用的实际JSON

$.get(  'yourFile.php',
        function(data){
            console.log(data);
        }
);
这不是JSON。您发布的内容看起来像是一个PHP数组,它用括号括起来,试图将其转换为JSON

用于将来验证JSON

现在,要将PHP数组转换为JSON,请使用它并将其发送到带有特定头的浏览器

$array = array( 'test' => 'sure' );
header('Content-type: application/json');
print json_encode($array);
exit;
现在,您将拥有可以在JavaScript中使用的实际JSON

$.get(  'yourFile.php',
        function(data){
            console.log(data);
        }
);

这不是JSON格式。这不是正确格式的JSON,它看起来更像一个PHP数组。这不是JSON。试试看!这不是JSON格式。这不是正确格式的JSON,它看起来更像一个PHP数组。这不是JSON。试试看!我已经看过这份报告了。我不知道该怎么做。你能更具体地告诉我,写一些吗codes@fish40添加了示例!我已经看过这份报告了。我不知道该怎么做。你能更具体地告诉我,写一些吗codes@fish40添加了示例!EnjoyThis是可以的,但是在ajax请求之后,使用内置功能在$中解析json更简单、更紧凑。ajax这是可以的,但是在ajax请求之后,使用内置功能在$中解析json更简单、更紧凑。ajax