我试图从JSON文件中获取随机字及其参数。
JSON文件:
{
ranWord1 {
sentence: "This is a sentence 1"
},
ranWord2 {
sentence: "This is a sentence 2"
},
ranWord3 {
sentence: "This is a sentence 3"
},
ranWord4 {
sentence: "This is a sentence 4"
},
ranWord5 {
sentence: "This is a sentence 5"
}
}
JS:
function readFiles() {
var result = null;
var file = 'dictionary.json';
$.ajax({
url: file,
type: 'get',
dataType: 'json',
async: false,
success: function(data) {
result = data;
}
});
var lines = result.split(", ");
var randLineNum = Math.floor(Math.random() * lines.length);
return lines[randLineNum];
}
我能够得到随机词,在这种情况下是ranWord1
,ranWord2
,ranWord3
,ranWord4
,ranWord5
。但是,我无法获得参数。我想将"This is a sentence 4"
存储在局部变量中。任何帮助将不胜感激!
更新
外部word.js文件:
var data = [
["ranWord1", "This is a sentence 1"],
["ranWord2", "This is a sentence 2"],
["ranWord3", "This is a sentence 3"],
["ranWord4", "This is a sentence 4"],
["ranWord5", "This is a sentence 5"]
]
JS:
function readFiles() {
var result = null;
var file = 'word.js';
$.ajax({
url: file,
type: 'get',
data: 'data',
dataType: 'script',
async: false,
success: function(data) {
var randomData = data[Math.floor(Math.random() * data.length)];
result = randomData;
alert("word: " + randomData[0] + ", sentence: " + randomData[1]);
}
});
}
我正在尝试从外部文件中获取数组,但现在我收到了undefined
的randomData [1],我收到了包括[
和]
在内的任何内容
答案 0 :(得分:2)
这应该可以胜任。
````
var randomSentence = '';
function readFiles() {
var result = null;
var file = 'dictionary.json';
$.ajax({
url: file,
type: 'get',
dataType: 'json',
async: false,
success: function(data) {
jsonSize = getObjectSize(data);
randNum = Math.floor(Math.random() * jsonSize);
randomSentence = data[randNum].sentence;
}
});
}
function getObjectSize(obj){
var size = 0, key;
for (key in obj) {
if (obj.hasOwnProperty(key)) size++;
}
return size;
}
理想情况下,您将在成功函数结束时进行函数调用,并将随机语句的结果传递给该函数,而不是使用全局变量。
答案 1 :(得分:2)
除了@AndyKillen解决的ajax问题,你的JSON无效。您可以使用http://jsonlint.com/进行检查。
这将是有效的JSON。
{
"ranWord1": {
"sentence": "This is a sentence 1"
},
"ranWord2": {
"sentence": "This is a sentence 2"
},
"ranWord3": {
"sentence": "This is a sentence 3"
},
"ranWord4": {
"sentence": "This is a sentence 4"
},
"ranWord5": {
"sentence": "This is a sentence 5"
}
}
同样在你的情况下你甚至不需要这么复杂的json。一个简单的数组就足够了:
[
"This is a sentence 1",
"This is a sentence 2",
"This is a sentence 3",
"This is a sentence 4",
"This is a sentence 5"
]
(除非你需要一些额外的数据) 这也将简化您的JavaScript代码。你可以这样做:
$.ajax({
url: file,
type: 'get',
dataType: 'json',
async: false,
success: function(data) {
var randomSentence = data[Math.floor(Math.random()*data.length)];
}
});
如果您需要这个词,您也可以这样做:
var data = [
["word1", "This is a sentence 1"],
["word2", "This is a sentence 2"],
["word3", "This is a sentence 3"],
["word4", "This is a sentence 4"],
["word5", "This is a sentence 5"]
]
var randomData = data[Math.floor(Math.random() * data.length)];
alert("word: " + randomData[0] + ", sentence: " + randomData[1]);
答案 2 :(得分:2)
尝试
var json = {
"ranWord1": {
"sentence": "This is a sentence 1"
},
"ranWord2": {
"sentence": "This is a sentence 2"
},
"ranWord3": {
"sentence": "This is a sentence 3"
},
"ranWord4": {
"sentence": "This is a sentence 4"
},
"ranWord5": {
"sentence": "This is a sentence 5"
}
};
function readFiles() {
var file = '/echo/json/';
var request = $.ajax({
url: file,
type: 'POST',
dataType: 'json',
data: {json:JSON.stringify(json)},
async: false
});
return request.then(function(data) {
var keys = Object.keys(data);
var sentences = $.map(data, function(rand, key) {
return rand.sentence
});
return [data, keys, sentences]
}, function(jqxhr, textStatus, errorThrown) {
return errorThrown
})
}
readFiles()
.then(function(response) {
console.log(response); // `response[0]`:`json`
var keys = response[1];
var sentences = response[2]
// do stuff with `keys`:"words" , `sentences`:"sentences"
console.log(keys, sentences);
// var lines = result.split(", ");
var randLineNum = Math.floor(Math.random() * keys.length);
console.log(keys[randLineNum]
, response[0][keys[randLineNum]]["sentence"]);
return keys[randLineNum]
+":"+ response[0][keys[randLineNum]]["sentence"];
}, function(errorThrown) {
console.log(errorThrown)
});
jsfiddle http://jsfiddle.net/j4141k6a/1/