简体   繁体   中英

Parsing an external JSON with an array and saving

I have a drupal website where articles are created then output as JSON to a particular link. I am currently trying to parse the JSON and save the title, body etc. of the articles on Parse Core. An example of the JSON output:

[
  {
    "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;"
  }
]

I partially based my code on this github . However due to the fact that the body object contains an array, I can not parse it any further and the direct body-value which contains the text I want cannot be saved.

I took a look at this Stackoverflow question and was still unable to solve the problem. An error was being returned Cannot read property 'length' of undefined . It is worth noting that the vid and title are saved successfully.

Furthermore only one of the posts is noted, the one with "vid" : "2" , no idea why it doesn't store the other one.

My main.js code:

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);
        }
    });

});

EDIT: This is the correct code excerpt that works, replacing the incorrect for loop above:

  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);
      }

Your JSON output is an Array of Objects.

So if your JSON data is contained in a variable, say, "jsonData", then from "jsonData" you can reach the "body" as follow,

jsonData[0].body

Then to get the array inside body you should go like,

jsonData[0].body.und

In your code you are not reaching the arrays properly, hence you are getting the length undefined error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM