解析外部JSON使用数组和保存 [英] Parsing an external JSON with an array and saving

查看:215
本文介绍了解析外部JSON使用数组和保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Drupal网站,创建文章,然后输出作为JSON到一个特定的链接。目前我正在试图解析JSON,节省解析核心的文章的标题,正文等。在JSON输出的一个例子:

  [
  {
    VID:2,
    UID:1,
    头衔:后2
    日志:,
    状态:1
    注释:0,
    促进:1
    粘性:0,
    NID:2,
    类型:文章,
    语言:UND
    创造:1435932743,
    改变:1436089990,
    tnid:0,
    翻译:0,
    revision_timestamp:1436089990,
    revision_uid:1,
    身体: {
      UND:[
        {
          值:在整数MI blandit存有malesuada consectetur ...
          总结:,
          格式化:plain_text
          safe_value:&所述p为H.;整数在英里blandit ipsum的malesuada consectetur ...&下; / P> \\ N,
          safe_summary:
        }
      ]
    },
    field_tag​​s:[],
    field_image:[],
    名:uknj
    图片:0,
    数据:B:0;
  },
  {
    VID:1,
    UID:1,
    头衔:样品邮报,
    日志:,
    状态:1
    注释:0,
    促进:1
    粘性:0,
    NID:1,
    类型:文章,
    语言:UND
    创造:1435931896,
    改变:1436090000,
    tnid:0,
    翻译:0,
    revision_timestamp:1436090000,
    revision_uid:1,
    身体: {
      UND:[
        {
          值:Lorem存有悲坐阿梅特...
          总结:,
          格式化:plain_text
          safe_value:< P> Lorem存有悲坐阿梅德...< / P> \\ N,
          safe_summary:
        }
      ]
    },
    field_tag​​s:[],
    field_image:[],
    名:uknj
    图片:0,
    数据:B:0;
  }
]

我部分基于我的code上的这个github上。然而,由于身体的对象包含数组的事实,我无法进一步分析它,并包含我想不能保存的文本直接身体的价值。

我看了一下这个问题#1 并仍然无法解决这个问题。正在返回一个错误无法读取未定义的属性长度。值得注意的是,VID和标题保存成功。

此外只有职位之一指出,一个与VID:2,不知道为什么它不存储另一个

我main.js code:

  VAR _ =要求(下划线);
Parse.initialize('某某','123');
VAR文章= Parse.Object.extend(公司章程);
VAR文章=新的文章();Parse.Cloud.job(ArticleFeed功能(请求,响应){
  Parse.Cloud.htt prequest({
        方法:GET,
        网址:'URL HERE,
        成功:函数(HTT presponse){
         VAR数据= JSON.parse(HTT presponse.text);
           对于(VAR I = 0; I<数据[I] .body.und.length;我++){
        文章=新的文章()
        内容=数据[I]
        article.set(身体,content.body.und [0] .value的);
        article.set(VID,content.vid);
        article.set(称号,content.title);
        VAR epochTime = content.created;
        VAR newDate = moment.utc(1234567890000);
        article.set(DATE_CREATED,newDate);        articles.push(条);
      }          article.save();            response.success(HTT presponse.text); //这将与HTTP响应的内容作出反应
        },
        错误:功能(HTT presponse){
            console.error(+ HTT presponse.status'请求与响应code失败');
            response.error(+ HTT presponse.status'请求与响应code失败');
        }
    });});

编辑:这是正确的code节选这样的作品,替换不正确的for循环以上:

 为(VAR I = 0; I<数据[I] .body.und.length;我++){
            文章=新的文章()
            内容=数据[I]
            article.set(身体,content.body.und [0] .value的);
            article.set(VID,content.vid);
            article.set(称号,content.title);            articles.push(条);
      }


解决方案

您JSON输出为对象的数组。

所以,如果你的JSON数据包含在一个变量,说:jsonData,再从jsonData可以达到身体为遵循,

  jsonData [0]。体

然后让体内的数组,你应该去喜欢,

  jsonData [0] .body.und

在您的code你是不是正确到达阵列,因此,你所得到的长度不确定的错误。

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.

这篇关于解析外部JSON使用数组和保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆