将json从文件加载到对象中

时间:2012-06-12 22:27:07

标签: javascript jquery json object

努力将json从URL上的文件(myData.json)加载到对象中,以便我可以访问属性值。

- 数据立即加载,我在应用程序中需要很多。

- 我将在整个应用程序中访问数据,而不仅仅是在数据加载后立即发生的一个函数的一部分。

- 我确保我文件中的数据格式正确json。

按照jquery API上的示例,我不应该做一些简单的事情:

警报(jqxhr.myProperty);

并获得价值? 我在这里错过了什么步骤?我尝试过运行eval和各种各样的事情,比如

var myObj = JSON.parse(jqxhr);

无济于事。

请....谢谢你。

// Assign handlers immediately after making the request,
// and remember the jqxhr object for this request
var jqxhr = $.getJSON("example.json", function() {
  alert("success");
})
.success(function() { alert("second success"); })
.error(function() { alert("error"); })
.complete(function() { alert("complete"); });

// perform other work here ...

// Set another completion function for the request above
jqxhr.complete(function(){ alert("second complete"); });

3 个答案:

答案 0 :(得分:12)

我认为你太复杂了。)

 var JSON;

 $.getJSON('example.json', function(response){
       JSON = response;
       alert(JSON.property);
 })
 //feel free to use chained handlers, or even make custom events out of them!
 .success(function() { alert("second success"); })
 .error(function() { alert("error"); })
 .complete(function() { alert("complete"); });

getJSON函数自动将您的响应转换为适当的JSON对象。无需解析。

您提到您在整个地方使用此数据,因此您必须等待ajax调用完成才能访问数据。这意味着要么将整个应用程序包装在getJSON回调中。或使用自定义事件来确定如下:

 var JSON;

 $(window).on('JSONready', function(){
       alert(JSON.property);
 });

 $.getJSON('example.json', function(response){
       JSON = response;
       $(window).trigger('JSONready');
 });

 $('#elem').on('click', function(){
       //event likely to take place after ajax call has transpired
       //it would still be better to assign this listener in a callback, 
       //but you can get away with not doing it, if you put in a catch
       if(JSON){
           alert(JSON.property);
       }          
 });

修改

快速调试后,数据不可用的真正原因是:消耗JSON的javascript位于文件中,包含执行调用的内联javascript的页面文件NORTH。因此,JSON不是全局变量,并且范围阻止了它的使用。如果你真的需要一个变量是全局的,那么它可以用于内联JS以及任何和所有包含的js文件,你可以这样做:

  (function(){
      var limitedScopeVariable = 25;
      window.globalScopeVariable = 30;
  })();

  $(function(){
       alert(globalScopeVariable); //works!
       alert(limitedScopeVariable); //fails!
  });

编辑2

  

从jQuery 3.0开始,回调函数是不同的:   jqXHR.success(),jqXHR.error()和jqXHR.complete()回调方法   从jQuery 3.0开始删除。你可以使用jqXHR.done(),jqXHR.fail(),   而jqXHR.always()改为

来自评论@ mario-lurig

答案 1 :(得分:3)

将json数据传递给$ .getJSON的回调函数。 所以这会奏效:

var jqxhr;

$.getJSON("example.json", function(data) {
  jqxhr = data;
});

// alert(jqxhr.property);
// caution: this won't work immediately on load, since the ajax call runs asynchronously and hasn't finished at that time

// it should be available at a later time, like a click event
$('a#something').click(function(){
     if(jqxhr){
          alert(jqxhr.property);
     }else{
          alert('getJSON not yet complete or failed');
     }
});

答案 2 :(得分:0)

我认为这就是你要找的东西,你试图访问你的呼叫返回的数据而不是调用者对象本身。在您的示例中,jqxhr是处理JSON调用而不是数据的对象。所以,

$.getJSON("example.json", function(data) {
  yourDATA = data;
})

//Use your data here
alert(yourDATA.aProperty);

this page上的第一个例子与我解释的相似。