jQuery返回的json数据为未定义且图像未加载

时间:2018-11-24 18:05:55

标签: javascript jquery json

我正在尝试访问json文件的最后一个数组,并从json文件的“数据”数组返回值,并将其放入choiceSelection数组中。但是,在我的本地主机上,它返回一个未定义的值,并且不会加载图像。谁能帮我吗?如果您不确定我的问题/逻辑原因,我深表歉意。因此,如果不确定,请向我询问更多详细信息。谢谢!

javascript代码

JavaRDD<String> words = sc.parallelize(Arrays.asList("1","2"));

JavaPairRDD<String, Double> pairRDD = words.mapToPair(s -> new Tuple2<>(s, 1.0));

JavaPairRDD<Integer, Double> pairRDD1 = pairRDD.mapToPair(f->new Tuple2<>(Integer.parseInt(f._1), 1.0));

json文件

const result = await sequelize.query("select id,email from users where 
      users.id in(SELECT friendId FROM friendships where friendships.userId = 
     ${userId})", {type: sequelize.QueryTypes.SELECT});

编辑1:将json [i]更改为json [2] .data。仍未定义 编辑2:更改了json [2] .data。到json [2] .data [i],并在for语句中使用了json [3] .data.length。现在,它可以完美运行。谢谢大家的帮助!:)

1 个答案:

答案 0 :(得分:1)

您可以消除代码中的麻烦,并使用一些ES6分解来更轻松地获取数据。

const json = '[{"name":"match numbers 1","template":"matching","data":[["six","Images/Number6.jpg"],["eight","Images/Number8.jpg"],["nine","Images/Number9.jpg"]]},{"name":"order numbers 1","template":"ordering","data":[["Images/Number6.jpg"],["Images/Number8.jpg"],["Images/Number9.jpg"]]},{"name":"animal","template":"picture game","data":[{"question":"Where is the cat?","correctChoice":"Images/5cats.jpg","choice1":"Images/squirrel.png","choice2":"Images/beagle.png"},{"question":"Where is the cat?","correctChoice":"Images/5cats.jpg","choice1":"Images/squirrel.png","choice2":"Images/beagle.png"}]}]'

function getJSON(endpoint, callback) {
  setTimeout(() => callback(JSON.parse(json)), 1000);
}

// grab the third object from the response data
getJSON('data.json', function([ ,,obj ]) {

  // grab the data array from that object but relabel it
  // `choiceSelection
  const { data: choiceSelection } = obj;

  // then use the object property keys to get access
  // to the data instead of indexes. Much easier.
  console.log(choiceSelection[0].question);
  console.log(choiceSelection[1].question);
});