2016-09-08 13 views
0

からのオブジェクトにアクセスできません:は、私はJSONオブジェクトを次しまったJSON

[{"model": "pricing.cashflow", "pk": 1, "fields": {"value": 4.0, "date": "2016-09-09"}}, {"model": "pricing.cashflow", "pk": 2, "fields": {"value": 3.0, "date": "2016-09-01"}}, {"model": "pricing.cashflow", "pk": 3, "fields": {"value": 3.0, "date": "2016-09-01"}}, {"model": "pricing.cashflow", "pk": 4, "fields": {"value": 3.0, "date": "2016-09-01"}}, {"model": "pricing.cashflow", "pk": 5, "fields": {"value": 3.0, "date": "2016-09-01"}}, {"model": "pricing.cashflow", "pk": 6, "fields": {"value": 5.0, "date": "2016-09-07"}}, {"model": "pricing.cashflow", "pk": 7, "fields": {"value": 3.0, "date": "2016-09-28"}}, {"model": "pricing.cashflow", "pk": 8, "fields": {"value": 3.0, "date": "2016-09-22"}}, {"model": "pricing.cashflow", "pk": 9, "fields": {"value": 5.0, "date": "2016-09-16"}}, {"model": "pricing.cashflow", "pk": 10, "fields": {"value": 5.0, "date": "2016-09-16"}}, {"model": "pricing.cashflow", "pk": 11, "fields": {"value": 4.0, "date": "2016-09-08"}}, {"model": "pricing.cashflow", "pk": 12, "fields": {"value": 8.0, "date": "2016-09-22"}}, {"model": "pricing.cashflow", "pk": 13, "fields": {"value": 3.0, "date": "2016-09-22"}}, {"model": "pricing.cashflow", "pk": 14, "fields": {"value": 5.0, "date": "2016-09-01"}}, {"model": "pricing.cashflow", "pk": 15, "fields": {"value": 8.0, "date": "2016-09-01"}}, {"model": "pricing.cashflow", "pk": 16, "fields": {"value": 4.0, "date": "2016-09-08"}}, {"model": "pricing.cashflow", "pk": 17, "fields": {"value": 5.0, "date": "2016-09-09"}}, {"model": "pricing.cashflow", "pk": 18, "fields": {"value": 5.0, "date": "2016-09-09"}}, {"model": "pricing.cashflow", "pk": 19, "fields": {"value": 5.0, "date": "2016-09-09"}}, {"model": "pricing.cashflow", "pk": 20, "fields": {"value": 3.0, "date": "2016-09-16"}}, {"model": "pricing.cashflow", "pk": 21, "fields": {"value": 3.0, "date": "2016-09-16"}}, {"model": "pricing.cashflow", "pk": 22, "fields": {"value": 3.0, "date": "2016-09-16"}}, {"model": "pricing.cashflow", "pk": 23, "fields": {"value": 3.0, "date": "2016-09-16"}}, {"model": "pricing.cashflow", "pk": 24, "fields": {"value": 3.0, "date": "2016-09-16"}}] 

私はフィールド「値」と「日付」を取得したいです。私は、JSONオブジェクトとしてデシリアライズ、JavaScriptで

$http.get("/getcashflow/") 
     .success(function(data) { 
      for(var cf in data){ 
       alert(cf.fields.value.data*); 
      } 
     }); 
    }; 
+1

。 data。*は返された元の配列を参照します。 cfは配列の単一のオブジェクトを参照します – mhodges

+0

@mhodgesは今のように?元の質問を参照してください。 – belgiums

+0

それは読む必要があります:alert(cf.fields.value) – mhodges

答えて

1

あなたがしようとしているかに基づいて、するには、for...inループではなく、.forEach()ループを使用する必要があります。配列上にfor...inを使用すると、各要素のインデックスが返されますが、for...inループを使用する目的を無効にするdata [cf]と言って元の配列に戻って参照しない限り、あなたの場合は役に立ちません。最初の場所。あなたがcf.fields.valueを言う必要が

var data = [{"model": "pricing.cashflow", "pk": 1, "fields": {"value": 4.0, "date": "2016-09-09"}}, {"model": "pricing.cashflow", "pk": 2, "fields": {"value": 3.0, "date": "2016-09-01"}}, {"model": "pricing.cashflow", "pk": 3, "fields": {"value": 3.0, "date": "2016-09-01"}}, {"model": "pricing.cashflow", "pk": 4, "fields": {"value": 3.0, "date": "2016-09-01"}}, {"model": "pricing.cashflow", "pk": 5, "fields": {"value": 3.0, "date": "2016-09-01"}}, {"model": "pricing.cashflow", "pk": 6, "fields": {"value": 5.0, "date": "2016-09-07"}}, {"model": "pricing.cashflow", "pk": 7, "fields": {"value": 3.0, "date": "2016-09-28"}}, {"model": "pricing.cashflow", "pk": 8, "fields": {"value": 3.0, "date": "2016-09-22"}}, {"model": "pricing.cashflow", "pk": 9, "fields": {"value": 5.0, "date": "2016-09-16"}}, {"model": "pricing.cashflow", "pk": 10, "fields": {"value": 5.0, "date": "2016-09-16"}}, {"model": "pricing.cashflow", "pk": 11, "fields": {"value": 4.0, "date": "2016-09-08"}}, {"model": "pricing.cashflow", "pk": 12, "fields": {"value": 8.0, "date": "2016-09-22"}}, {"model": "pricing.cashflow", "pk": 13, "fields": {"value": 3.0, "date": "2016-09-22"}}, {"model": "pricing.cashflow", "pk": 14, "fields": {"value": 5.0, "date": "2016-09-01"}}, {"model": "pricing.cashflow", "pk": 15, "fields": {"value": 8.0, "date": "2016-09-01"}}, {"model": "pricing.cashflow", "pk": 16, "fields": {"value": 4.0, "date": "2016-09-08"}}, {"model": "pricing.cashflow", "pk": 17, "fields": {"value": 5.0, "date": "2016-09-09"}}, {"model": "pricing.cashflow", "pk": 18, "fields": {"value": 5.0, "date": "2016-09-09"}}, {"model": "pricing.cashflow", "pk": 19, "fields": {"value": 5.0, "date": "2016-09-09"}}, {"model": "pricing.cashflow", "pk": 20, "fields": {"value": 3.0, "date": "2016-09-16"}}, {"model": "pricing.cashflow", "pk": 21, "fields": {"value": 3.0, "date": "2016-09-16"}}, {"model": "pricing.cashflow", "pk": 22, "fields": {"value": 3.0, "date": "2016-09-16"}}, {"model": "pricing.cashflow", "pk": 23, "fields": {"value": 3.0, "date": "2016-09-16"}}, {"model": "pricing.cashflow", "pk": 24, "fields": {"value": 3.0, "date": "2016-09-16"}}]; 
 

 
data.forEach(function (cf) { 
 
    console.log(cf.fields.value); 
 
});

0

...(?通常は良い方法は何ですか)このようにそれを実行しようとしましたが、動作しません。そのオブジェクトのメンバーにアクセスするには、ドットアクセサを使用します。あなたが探している値は、data[cf].fields.value.data

となります。for inループは実際の値ではなく、オブジェクトまたは配列のキーを返します。

$http.get("/getcashflow/") 
     .success(function(data) { 
      for(var cf in data){ 
       alert(data[cf].fields.value.data); 
      } 
     }); 
    }; 
1

、純粋なJS(ES6)に(ループの最初の反復では、たとえば、CFは、第一の目的はゼロに等しくない):

var input = [ 
    {"model": "pricing.cashflow", "pk": 1, "fields": {"value": 4.0, "date": "2016-09-09"}}, 
    {"model": "pricing.cashflow", "pk": 2, "fields": {"value": 3.0, "date": "2016-09-01"}}, 
    {"model": "pricing.cashflow", "pk": 3, "fields": {"value": 3.0, "date": "2016-09-01"}}, 
    {"model": "pricing.cashflow", "pk": 4, "fields": {"value": 3.0, "date": "2016-09-01"}} 
    ]; 
input.forEach(item => {console.log("value -> " + item.fields.value + " and date -> " + item.fields.date)}); 

// value -> 4 and date -> 2016-09-09 
// value -> 3 and date -> 2016-09-01 
// value -> 3 and date -> 2016-09-01 
// value -> 3 and date -> 2016-09-01 
+0

正しいES6ソリューションのために上がる – mhodges

関連する問題