使用数组解析外部JSON并保存

时间:2015-07-06 13:26:33

标签: javascript arrays json parsing parse-platform

我有一个drupal网站,其中创建文章然后作为JSON输出到特定链接。我目前正在尝试解析JSON并保存Parse Core上文章的标题,正文等。 JSON输出的一个示例:

[
  {
    "vid": "2",
    "uid": "1",
    "title": "Post 2",
    "log": "",
    "status": "1",
    "comment": "0",
    "promote": "1",
    "sticky": "0",
    "nid": "2",
    "type": "article",
    "language": "und",
    "created": "1435932743",
    "changed": "1436089990",
    "tnid": "0",
    "translate": "0",
    "revision_timestamp": "1436089990",
    "revision_uid": "1",
    "body": {
      "und": [
        {
          "value": "Integer at mi blandit ipsum malesuada consectetur...",
          "summary": "",
          "format": "plain_text",
          "safe_value": "<p>Integer at mi blandit ipsum malesuada consectetur...</p>\n",
          "safe_summary": ""
        }
      ]
    },
    "field_tags": [],
    "field_image": [],
    "name": "uknj",
    "picture": "0",
    "data": "b:0;"
  },
  {
    "vid": "1",
    "uid": "1",
    "title": "Sample Post",
    "log": "",
    "status": "1",
    "comment": "0",
    "promote": "1",
    "sticky": "0",
    "nid": "1",
    "type": "article",
    "language": "und",
    "created": "1435931896",
    "changed": "1436090000",
    "tnid": "0",
    "translate": "0",
    "revision_timestamp": "1436090000",
    "revision_uid": "1",
    "body": {
      "und": [
        {
          "value": "Lorem ipsum dolor sit amet...",
          "summary": "",
          "format": "plain_text",
          "safe_value": "<p>Lorem ipsum dolor sit amet...</p>\n",
          "safe_summary": ""
        }
      ]
    },
    "field_tags": [],
    "field_image": [],
    "name": "uknj",
    "picture": "0",
    "data": "b:0;"
  }
]

我部分基于this github的代码。但是由于body对象包含一个数组,我无法进一步解析它,并且无法保存包含我想要的文本的直接body-value。

我看了this Stackoverflow question但仍无法解决问题。正在返回错误Cannot read property 'length' of undefined。值得注意的是,vid和标题已成功保存。

此外,只有一个帖子被注明,一个帖子有"vid" : "2",不知道它为什么不存储另一个帖子。

我的main.js代码:

var _ = require("underscore");
Parse.initialize('xyz', '123');
var Articles = Parse.Object.extend("Articles");
var article = new Articles();

Parse.Cloud.job("ArticleFeed", function(request, response) {
  Parse.Cloud.httpRequest({
        method: 'GET',
        url: 'URL HERE',
        success: function(httpResponse) {
         var data= JSON.parse(httpResponse.text);
           for (var i = 0; i < data[i].body.und.length; i++) {
        article = new Articles(),
        content = data[i];
        article.set("body", content.body.und[0].value);
        article.set("vid", content.vid);
        article.set("title", content.title);
        var epochTime = content.created;
        var newDate = moment.utc(1234567890000);
        article.set("date_created", newDate);

        articles.push(article);
      }

          article.save();

            response.success(httpResponse.text); // This will respond with the contents of the http response
        },
        error: function(httpResponse) {
            console.error('Request failed with response code ' + httpResponse.status);
            response.error('Request failed with response code ' + httpResponse.status);
        }
    });

});

编辑:这是正确的代码摘录,取代上面不正确的for循环:

  for (var i = 0; i < data[i].body.und.length; i++) {
            article = new Articles(),
            content = data[i];
            article.set("body", content.body.und[0].value);
            article.set("vid", content.vid);
            article.set("title", content.title);

            articles.push(article);
      }

1 个答案:

答案 0 :(得分:1)

您的JSON输出是一个对象数组。

因此,如果您的JSON数据包含在变量中,比如说&#34; jsonData&#34;,那么来自&#34; jsonData&#34;你可以到达&#34;身体&#34;如下,

jsonData[0].body

然后将数组放入体内,你应该这样,

jsonData[0].body.und

在您的代码中,您没有正确访问数组,因此您得到未定义的长度错误。