2017-12-14 9 views
0

JSON配列内のオブジェクトの量に依存する一定量の環境変数を作成する必要があります。それらの変数はもちろん、異なる名前を付ける必要があります。私は以下を試しましたが、変数を作成することができません。Postman(JavaScript) - JSON配列に含まれるオブジェクトの数に基づいて環境変数を作成するにはどうすればよいですか?

var jsonstuff = JSON.parse(responseBody); 
for (var i = 0; i < jsonstuff.bullets.length; i++){ 
    postman.clearEnvironmentVariable("Bullet" + (i+1)); 
    postman.setEnvironmentVariable("Bullet" + (i+1), jsonstuff.bullets[i]); 
} 

私はJavascriptの新機能ですので、どんな情報でも、些細なことに感謝します!

答えて

0

あなたのコード私にはよさそうだ...私は私の側にいくつかのデータを嘲笑し、それが働きました元気ときは、次の

var responseBody = { 
 
    "bullets": [ 
 
    { 
 
     "_id": "5a32c9b400bf7e499ca242f2", 
 
     "index": 0, 
 
     "guid": "69ad4f73-b355-4268-94ef-b92f6cab505b", 
 
     "picture": "http://placehold.it/32x32" 
 
    }, 
 
    { 
 
     "_id": "5a32c9b482a6a89661d98e85", 
 
     "index": 1, 
 
     "guid": "6c8a1628-3fa9-4b52-b8d3-5719cd3889f7", 
 
     "picture": "http://placehold.it/32x32" 
 
    }, 
 
    { 
 
     "_id": "5a32c9b4610f9bb923a01a28", 
 
     "index": 2, 
 
     "guid": "7084aa50-dc85-410c-8dbb-02f860c3d97a", 
 
     "picture": "http://placehold.it/32x32" 
 
    }, 
 
    { 
 
     "_id": "5a32c9b43c17b09d2e5d819e", 
 
     "index": 3, 
 
     "guid": "5d076aa8-af3a-4af1-bf49-e62ade7c3ed0", 
 
     "picture": "http://placehold.it/32x32" 
 
    }, 
 
    { 
 
     "_id": "5a32c9b48594eea1e008c190", 
 
     "index": 4, 
 
     "guid": "5d1f4bcb-0d59-4acc-af0a-4041a1aefb7f", 
 
     "picture": "http://placehold.it/32x32" 
 
    } 
 
    ] 
 
}; 
 

 
//because my example is already an object, it does not need to be parsed into a javascript object 
 
var jsonstuff = responseBody; 
 

 
for (var i = 0; i < jsonstuff.bullets.length; i++){ 
 
    postman.clearEnvironmentVariable("Bullet" + (i+1)); 
 
    //this is the key change. without JSON.stringify(), the environment varible will be set to [Object object] 
 
    postman.setEnvironmentVariable("Bullet" + (i+1), JSON.stringify(jsonstuff.bullets[i])); 
 
}

注:JSOなしでN.stringify、環境変数を「[オブジェクトオブジェクト]」に設定していました

+1

ありがとうございました!それはあなたが言ったように正確に働いた。将来の努力のために注意を念頭に置いています。 –

+0

驚くばかり!あなたがあなたの方法でいることを聞いてうれしい:) –

1

ここでも実際のエキスパートはいませんが、私はいつもpm.environment.set("... name ...", jsonData.someProperty);を使用しています。私はあなたが使用しているインデックスを試していません。

それとは別に、あなたのコードに多少の誤差があるかもしれません、あなたは.lengthvarが欠落しています

for (var i = 0; i < jsonstuff.bullets.length; i++) { 
+1

ありがとう!あなたが指摘したエラーを修正しました。もう一度実行するが、同じ問題が残っている。あなたの助けが大いにありがとう! –

関連する問題