【问题标题】:Parsing & accessing JSON data解析和访问 JSON 数据
【发布时间】:2011-04-04 20:41:40
【问题描述】:

我有 JSON 数据(为可读性添加了一个换行符)返回为

[{"pk": 7, "model": "pycourt_login.orders", "fields": {"status": 0, "delivered": false, "order_id": "6count1%2", "student_id": 5, "counterid": "counter1", "datetime": "2011-04-05 01:44:01", "dish": 6, "transaction_id": "2008010661301935441", "quantity": 2}},
{"pk": 8, "model": "pycourt_login.orders", "fields": {"status": 0, "delivered": false, "order_id": "9count1%1", "student_id": 5, "counterid": "counter1", "datetime": "2011-04-05 01:44:14", "dish": 9, "transaction_id": "2008010661301935454", "quantity": 1}}]

如何访问 jQuery 中的每个项目。

我尝试做类似的事情(假设 /pendingorders 返回 JSON)

$(document).ready(function (){ 
jQuery.ajax({
    cache: false,
    url: "/pendingorders",
    type: "GET",
    success: function(json) {
        $.each(json.results, function(i,dish){
            alert(dish.pk); 
            },'json')
        },  
    }); 
});

这是我在 Firebug 中遇到的错误:

object is undefined
[Break On This Error] length = object.length,
jquery.js (line 605)

【问题讨论】:

    标签: javascript jquery json parsing


    【解决方案1】:

    以目前的结构,我想你只是想要:

    jQuery.ajax({
        cache: false,
        url: "/pendingorders",
        type: "GET",
        success: function(json) {
            $.each(json, function(i,dish){
                  alert(dish.pk); 
            });
        }); 
    });
    

    你有一个流浪的, 'json' 作为each 的第三个参数,成功值后面有一个逗号(这没关系,但可能是无意的)

    但是,由于JSON hijacking,您可能希望将顶层设为对象而不是数组。

    如果结构是:

    {"results":
    [{"pk": 7, "model": "pycourt_login.orders", "fields": {"status": 0, "delivered": false, "order_id": "6count1%2", "student_id": 5, "counterid": "counter1", "datetime": "2011-04-05 01:44:01", "dish": 6, "transaction_id": "2008010661301935441", "quantity": 2}},
    {"pk": 8, "model": "pycourt_login.orders", "fields": {"status": 0, "delivered": false, "order_id": "9count1%1", "student_id": 5, "counterid": "counter1", "datetime": "2011-04-05 01:44:14", "dish": 9, "transaction_id": "2008010661301935454", "quantity": 1}}]}
    

    你原来的每一行:

    $.each(json.results, function(i,dish){
    

    应该是正确的。

    【讨论】:

    • 感谢您提供翔实的回答。我认为“.results”是使其可迭代的某种方法。
    • 如果我们可以做json劫持,我想知道为什么我在django中序列化数据库查询的结果时会得到那种结果。他们应该为此做点什么。
    【解决方案2】:

    您将“json”作为参数传递给each()(注意缩进)。您通常可以在 get() 上执行此操作,但 ajax() 的工作方式不同 - 设置 dataType 选项就足够了:

    jQuery.ajax({
        cache: false,
        url: "/pendingorders",
        type: "GET",
        dataType: 'json',
        success: function(json) {
            $.each(json.results, function(i,dish){
                alert(dish.pk); 
            });
        }
    });
    

    请参阅ajax method documentation 以供参考。

    【讨论】:

    • 我相信这是正确的答案。我只是想补充一点,如果您将服务器端 /pendingorders 代码配置为使用 application/json HTTP 标头进行响应,jQuery 将自动将响应视为 JSON 并在没有指定 dataType 的情况下对其进行反序列化。最终结果相同,但更容易一些。
    • 我将 json 作为 mimetype='application/json' 返回,所以我不需要 dataType。真正的错误是“.results”不应该存在。我写它是因为我认为这是使它可迭代的某种方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-15
    相关资源
    最近更新 更多