我有一个dousmanic长度的JSON对象,需要处理它

时间:2016-08-24 08:42:56

标签: javascript json

我有一个JSON响应,就像这样::

   {
    "abc": [{
        "statsFetchDate": 1463961600000,
        "facebookLikes": 0,
        "facebookComments": 0,
        "facebookShares": 0,
        "twitterShares": 0,
        "linkedInShares": null,
        "instagramLikes": null,
        "instagramComments": null,
        "engagement": null,
        "velocity": 50710.29999999996
    }, {
        "statsFetchDate": 1464048000000,
        "facebookLikes": 0,
        "facebookComments": 0,
        "facebookShares": 0,
        "twitterShares": 0,
        "linkedInShares": null,
        "instagramLikes": null,
        "instagramComments": null,
        "engagement": null,
        "velocity": 473968.7000000002
    }],
    "def": [{
        "statsFetchDate": 1463961600000,
        "facebookLikes": 0,
        "facebookComments": 0,
        "facebookShares": 0,
        "twitterShares": 0,
        "linkedInShares": null,
        "instagramLikes": null,
        "instagramComments": null,
        "engagement": null,
        "velocity": 50710.29999999996
    }, {
        "statsFetchDate": 1464048000000,
        "facebookLikes": 0,
        "facebookComments": 0,
        "facebookShares": 0,
        "twitterShares": 0,
        "linkedInShares": null,
        "instagramLikes": null,
        "instagramComments": null,
        "engagement": null,
        "velocity": 473968.7000000002
    }]
}

我需要为每个对象绘制图形,即在这种情况下为abc和def,但是json对象本质上是动态的,我可以获得多个对象,我如何能够通过每个对象进行处理并处理它们例如,我需要访问abc.stats获取日期和速度以及def.stats获取日期和速度,它将是一个多线图,绘图不是张力,但我如何解析多个JSON对象?

3 个答案:

答案 0 :(得分:0)

使用键循环对象,而不知道有多少个对象。

for(var key in object){
    //first key will be "abc", second "def"
    //object[key] will return the current object content
}

答案 1 :(得分:0)

检查下面的代码,console.log嵌套JSON中的每个statsFetchDate。 查看Fiddle

var obj = {
  "abc": [{
    "statsFetchDate": 1463961600000,
    "facebookLikes": 0,
    "facebookComments": 0,
    "facebookShares": 0,
    "twitterShares": 0,
    "linkedInShares": null,
    "instagramLikes": null,
    "instagramComments": null,
    "engagement": null,
    "velocity": 50710.29999999996
  }, {
    "statsFetchDate": 1464048000000,
    "facebookLikes": 0,
    "facebookComments": 0,
    "facebookShares": 0,
    "twitterShares": 0,
    "linkedInShares": null,
    "instagramLikes": null,
    "instagramComments": null,
    "engagement": null,
    "velocity": 473968.7000000002
  }],
  "def": [{
    "statsFetchDate": 1463961600000,
    "facebookLikes": 0,
    "facebookComments": 0,
    "facebookShares": 0,
    "twitterShares": 0,
    "linkedInShares": null,
    "instagramLikes": null,
    "instagramComments": null,
    "engagement": null,
    "velocity": 50710.29999999996
  }, {
    "statsFetchDate": 1464048000000,
    "facebookLikes": 0,
    "facebookComments": 0,
    "facebookShares": 0,
    "twitterShares": 0,
    "linkedInShares": null,
    "instagramLikes": null,
    "instagramComments": null,
    "engagement": null,
    "velocity": 473968.7000000002
  }]
}
var objKeys = Object.keys(obj)
for (var i = 0, len = objKeys.length; i < len; i++) {
  for (var y = 0, len = obj[objKeys[i]].length; y < len; y++) {
    console.log(obj[objKeys[i]][y].statsFetchDate)
  }
}

答案 2 :(得分:0)

要处理上述数据结构,您需要执行以下步骤。

<强> 1。解析JSON字符串

var oJSON = JSON.parse(jsonResponseString);

<强> 2。迭代(动态)对象属性

for (var prop in oJSON) {
    if (oJSON.hasOwnProperty(prop)) {
        var aData = oJSON[prop]; // aData will be "abc" for example
        // do something with the array
    }
}

您需要使用hasOwnProperty检查属性,以避免访问toString()等继承属性。 https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty

第3。对数据进行绘图

// For example
aData.forEach(function(dataPoint) {
    // dataPoint is an object
    plotPoint(dataPoint.statsFetchDate, dataPoint.velocity);
});

// alternative
for (var i = 0; i < aData.length; i++) {
    var dataPoint = aData[i]; // This is an object
    plotPoint(dataPoint.statsFetchDate, dataPoint.velocity);
}