【问题标题】:extracting data from json string returns an error从 json 字符串中提取数据返回错误
【发布时间】:2014-12-27 20:28:24
【问题描述】:

我在 php 文件中有这段代码

[
{
    "_id": "549f065925e1928098c74275",
    "index": 0,
    "guid": "c21b3720-430e-4be3-9309-e8afbabc0020",
    "email": "browningriley@zilodyne.com",
    "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": "bonitasharp@medalert.com",
    "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: invalid 'in' operand a”,您可以在此处看到in this online example。 php文件是data.php,代码在script.js文件中

【问题讨论】:

  • 我真诚地希望这些信息不是真实人物的真实数据!
  • 不,它来自这里 json-generator.com 只是随机的,不是真正的 json 数据
  • 使用$.getJSON 而不是$.get

标签: php jquery arrays json


【解决方案1】:

您只需将dataType 设置为'json' 或使用自动设置数据类型的$.getJSON()

这将告诉$.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

【讨论】:

    【解决方案2】:

    您需要解析您的 php 响应:
    data = JSON.parse(data)
    请参阅此plunker(第 7 行已更新)

    原因是你在响应中得到一个字符串,然后你需要将它解析为JSON,你按照上面的例子来做。

    【讨论】:

    • 让 jQuery 在内部进行解析的好处是您不需要创建自己的 try/catch 以防 json 字符串无效,而是触发 ajax 错误回调
    • @charlietfl,你是对的,但最好了解 jquery 背后发生的事情(一点点)
    • 但这是我的观点的一部分,使用内部来帮助捕获错误,而不必自己捕获它们。您的解决方案未涵盖所需的额外错误处理
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多