getJSon然后不工作

时间:2016-03-30 11:37:25

标签: javascript jquery json getjson

我正在尝试使用getJson函数从Json文件中获取数据:

  $.getJSON( "uploads/data.json" ).then(function(data) {
    allData = data.sculptures;
    locations = {};
    for(var i=0; i<data.location.length; i++) {
        locations[data.location[i].name] = data.location[i].location;
    }
    console.log(allData);
    console.log(locations);
});

但我没有得到任何东西。当我在Chrome控制台开发人员工具中运行此代码时,它只显示以下内容:

  Object {}
        always: ()
        done: ()
        fail: ()
        pipe: ()
        progress: ()
        promise: (a)
        state: ()
        then: ()
        __proto__: Object

这是data.json:

{"sculptures":[{"title":"Bust of Caracalla","location":"Paris"},
               {"title":"Brutus The Younger","location":"Paris"}],
 "location":[{"name":"Beaux-Arts in Dijon, France","location":{"lat":47,"lng":5}},
             {"name":"Louvre, Paris","location":{"lat":48,"lng":2}},
             {"name":"St Pauls Cathedral, London","location":{"lat":51,"lng":-0}},
             {"name":"V&A, London","location":{"lat":51,"lng":-0}}]}

1 个答案:

答案 0 :(得分:-1)

 locations = [];
  $.ajax({
    type: "POST",
    url: "uploads/data.json",
    dataType: "json",
    success: function(data) {
        $(data.location).each(function(i, location){
            locations[data.location[i].name] = data.location[i].location;
        });
    }
});