2016-08-24 9 views
-3

を処理する必要がある::私は、長さがdymanicある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にアクセスする必要があります。 .statsは日付と速度を取得しますが、それは複数の折れ線グラフになります。プロットは緊張ではありませんが、どうすれば複数のJSONオブジェクトを解析できますか?

+1

の可能性が含まれたデータ、[このfor..in]であなたのプロットを行います参照/ステートメント/ for ... in)、[Object.keys](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/keys)および[for..of] (https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/for...of)オブジェクトを反復可能にする場合 – Xotic750

答えて

0

あなたのネストされたJSON内の各statsFetchDateconsole.logそれ以下のコードをチェックしてください。 チェック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) 
 
    } 
 
}

+0

ありがとうございました!私は今問題を持っている..:D –

0

オブジェクトの数がわからず、キーを使用してオブジェクトを循環させます。

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

次の手順を実行する必要があり、上記のようなデータ構造を処理するには。

1.

var oJSON = JSON.parse(jsonResponseString); 

2. JSON文字列を解析あなたが継承されたアクセスを避けるためにhasOwnPropertyとプロパティをチェックする必要があります(動的な)オブジェクトのプロパティ

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

以上の反復toString()のようなプロパティ。 https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty

3. https://developer.mozilla.org/en/docs/Web/JavaScript/(

// 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); 
} 
関連する問題