从json字符串中提取数据返回错误 - php

我在PHP文件中有此代码

[
{
    "_id": "549f065925e1928098c74275",
    "index": 0,
    "guid": "c21b3720-430e-4be3-9309-e8afbabc0020",
    "email": "[email protected]",
    "phone": "+1 (804) 417-2615",
    "address": "657 Temple Court, Katonah, Mississippi, 7139",
    "about": "Ut laborum ut nostrud dolore ut aute irure aliquip duis. Amet proident fugiat cupidatat nulla ullamco adipisicing ea excepteur. Proident in ullamco aute reprehenderit ea. Consequat non cupidatat id sit nostrud non. Velit amet exercitation incididunt aliqua deserunt cupidatat et ex.\r\n",
    "registered": "2014-09-05T01:48:12 -03:00",
    "latitude": -44.882099,
    "longitude": 24.574332,
    "tags": [
        "officia",
        "consectetur",
        "incididunt",
        "eu",
        "magna",
        "esse",
        "elit"
    ],
    "friends": [
        {
            "id": 0,
            "name": "Billie Jarvis"
        },
        {
            "id": 1,
            "name": "Laurie Espinoza"
        },
        {
            "id": 2,
            "name": "Kate Stuart"
        }
    ],
    "greeting": "Hello, Browning Riley! You have 6 unread messages.",
    "favoriteFruit": "banana"
},
{
    "_id": "549f065925aa17df2fd6ebea",
    "index": 1,
    "guid": "9cf247e8-fe6b-4c42-a4a3-24ef7b907ad4",
    "email": "[email protected]",
    "phone": "+1 (946) 506-2141",
    "address": "414 Willoughby Avenue, Gila, California, 4696",
    "about": "Aliqua aute tempor veniam sit esse velit anim. Proident amet aliqua ad non labore eu voluptate labore in amet exercitation irure. Qui laborum ea aliqua consectetur minim aliqua amet minim laborum sint fugiat ullamco nulla elit.\r\n",
    "registered": "2014-02-11T20:29:39 -02:00",
    "latitude": -19.03677,
    "longitude": 138.137275,
    "tags": [
        "eu",
        "non",
        "et",
        "nostrud",
        "enim",
        "proident",
        "sint"
    ],
    "friends": [
        {
            "id": 0,
            "name": "Gamble Porter"
        },
        {
            "id": 1,
            "name": "Jami Bell"
        },
        {
            "id": 2,
            "name": "Mullen Alexander"
        }
    ],
    "greeting": "Hello, Bonita Sharp! You have 5 unread messages.",
    "favoriteFruit": "strawberry"
}
]

现在尝试从该字符串中提取数据,如下所示

 $("#get").on('click',function(){

            $.get('data.php',function(data){
                console.log(data);
                var json_array = data;
                var new_array = [];
                $.each(json_array,function(i,o) {
                 new_array.push(o._id);
                });

                console.log(new_array);

        });
    });

但这是结果“ TypeError:无效的'in'操作数a”,如您在此处in this online example所见。 php文件是data.php,代码在script.js文件中

参考方案

您只需将dataType设置为'json'或使用$.getJSON()即可自动设置dataType。

这将告诉$.ajax期望什么并相应地进行解析

     $.get('data.php',function(data){

                var myarray = data;
                console.log(myarray);
                var new_array = [];
                $.each(myarray,function(i,o) {
                 new_array.push(o._id);
                });

                console.log(new_array);


            $("#result").text(new_array);
        },'json');/* last argument is "dataType" */

文件的内容标头未作为application/json发送

DEMO

PHP-MySQL结果转换为JSON - php

我试图了解如何将MySQL结果转换为JSON格式,以便以后可以在Javascript中使用此JSON来构建HTML表。但是我的代码只是产生大量的空值,我还不明白为什么。$result = mysqli_query($con, "SELECT * FROM Customers"); $test = json_encode($result);…

json_encode网址失败 - php

有人在this bug附近吗?echo json_encode(array('url'=>'/foo/bar')); {"url":"\/foo\/bar"} 我使用Zend_Json and Zend_Json_Expr以便我甚至可以在js对象中获取回调函数-但我无法获得…

json_encode的特定方式 - php

所以这是我的PHP<?php include 'connect.php'; $sql = "SELECT name from startup_schema"; $result = mysqli_query($mysqli, $sql) or die("Error in Selecting " …

PHP:将数据从二维数组复制到一维数组的最快方法 - php

我有一个巨大的二维PHP数组,带有500万行。$t = [ [ "id" => 1, "name" => "foo" ], [ "id" => 2, "name" => "bar" ] ]; 现在,我必须将此数组的I…

PHP Count数组元素 - php

嗨,有人可以解释为什么这会返回“数组由0个元素组成”。 :$arr = array(1,3,5); $count = count($arr); if ($count = 0) { echo "An array is empty."; } else { echo "An array has $count elements.…