コンソールでは、「男性」または「女性」の値を記録できないようです。それはconsole.log(choice[0].a)
のようなものでなければなりません。変数string
の値は記録されません。以下は私が試したことです、あなたの時間をありがとう。javascript変数を読み取ることができません
<html>
<head>
<script type="text/javascript">
var string = "";
var randoml = "ab";
while (string.length < 1) {
string += randoml[Math.floor(Math.random() * randoml.length)];
}
console.log(string);
data = '[{"a" : "male", "b" : "female"}]';
var choice = JSON.parse(data);
console.log(choice[0].string);
</script>
</head>
</html>
は、なぜあなたはそれをJSON.parseする文字列を作成するのですか?オブジェクトを作成するだけで、はるかに簡単になります – baao