【问题标题】:Parsing JSON array of objects: undefined property解析对象的 JSON 数组:未定义的属性
【发布时间】:2015-12-01 08:51:00
【问题描述】:

因此,类似于我之前的问题here(我不确定是提出新问题还是编辑旧问题),我设法使用下面的代码成功解析了body JSON 响应。

但是,当尝试对 event_calendar_date 执行相同操作时,我得到了:Failed with: TypeError: Cannot read property 'und' of undefined。我会假设要到达数组中的下一个对象,两者的方法都是相同的,但事实并非如此。

来自服务器的 JSON 响应:

[

    {
        "revision_uid":"1",
        "body":{
            "und":[
                {
                    "value":"Blah Blah.",
                    "summary":"",
                    "format":null,
                    "safe_value":"Blah Blah.",
                    "safe_summary":""
                }
            ]
        },
        "event_calendar_date":{
            "und":[
                {
                    "value":"2015-07-20 14:00:00",
                    "value2":"2015-07-20 20:00:00",
                    "timezone":"America/New_York",
                    "timezone_db":"America/New_York",
                    "date_type":"datetime"
                }
            ]
        }
    }

]

我的代码:

Parse.Cloud.httpRequest({
    method: "GET",
    url: 'http://www.scolago.com/001/articles/views/articles',
    success: function(httpResponse) {
        var data = JSON.parse(httpResponse.text);
        var articles = new Array();
        for (var i = 0; i < data.length; i++) {
            var Articles = Parse.Object.extend("Articles"),
                article = new Articles(),
                content = data[i];

            article.set("body", content.body.und[0].value);
            article.set("vid", content.vid);
            article.set("title", content.title);

            article.set("events", content.event_calendar_date.und[0].value);

            articles.push(article);
        };


        // function save(articles);
        Parse.Object.saveAll(articles, {
            success: function(objs) {
                promise.resolve();
            },
            error: function(error) {
                console.log(error);
                promise.reject(error.message);
            }
        });

    },
    error: function(error) {
        console.log(error);
        promise.reject(error.message);
    }
});

【问题讨论】:

  • 对 var 数据做一个 console.log,它会在检查器中显示数据的结构,然后你可以看到它有什么。这将帮助您调试问题

标签: javascript json parse-platform


【解决方案1】:

恐怕您没有给我们所有代码,或者您使用的不是您在此问题中发布的代码,因为它应该可以正常运行,如您所见in this demo

for (var i = 0; i < data.length; i++) {
    var content = data[i];
    console.log(content.event_calendar_date.und[0].value);
};

编辑和解决方案

正如我所料,您得到的 JSON 没有用于第二个和第三个对象的字段 event_calendar_date。我检查了你的full code on GitHub.

第一个对象有正确的event_calendar_date 字段,但下一个记录没有。看看 JSON 是如何格式化的 - http://www.jsoneditoronline.org/?id=b46878adcffe92f53f689978873a3474

您需要先检查当前循环迭代中的记录中是否存在content.event_calendar_date,或者将event_calendar_date 添加到JSON 响应中的所有记录中。

【讨论】:

  • 好吧,here 是完整的代码,如果您认为它会有所帮助的话。但是,afaik,我发布了所有相关代码。
  • 我不相信我能够更改 JSON 响应,那么检查 content.event_calendar_date 的简单 if 语句是否适合该任务?感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 2012-08-26
  • 1970-01-01
  • 2016-01-29
  • 2018-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多