【问题标题】:load json from file into object将json从文件加载到对象中
【发布时间】:2012-06-15 20:57:00
【问题描述】:

努力将 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"); });

【问题讨论】:

    标签: javascript jquery json object


    【解决方案1】:

    我觉得你太复杂了:)

     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() 代替

    来自 cmets @mario-lurig

    【讨论】:

    • 那行不通。 alert(JSON.property); 在调用回调之前执行。
    • 和 id 喜欢错误处理和成功功能。为什么我使用这个例子。
    • 您可以随意使用它们;)您希望我将它们复制并粘贴到我的答案中吗?
    • fresheyeball,我打算用你编辑的代码试试这个...brb。
    • 从 jQuery 3.0 开始,回调函数有所不同:从 jQuery 3.0 开始,jqXHR.success()、jqXHR.error() 和 jqXHR.complete() 回调方法被删除。您可以改用 jqXHR.done()、jqXHR.fail() 和 jqXHR.always()。
    【解决方案2】:

    将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');
         }
    });
    

    【讨论】:

    • 我正在加载在 document.onLoad() 之前发生的 jquery 调用中的数据;火灾,所以我期待它会可用。直到 onLoad(); 才会显示开始按钮
    • @Fresheyeball 那么指出可能的陷阱并在下面一行提出解决方案有什么问题?
    • 因为你展示的东西绝对不会成为可能的陷阱。很好地解释为什么可能的解决方案是必要的,但这就是它应该的。它不应该包括我们都知道行不通的技术的演示。评论那行代码,并指出它是一个不起作用的例子,我会支持你。
    • andy,我正在尝试此代码(请参阅我的答案),但无法访问 .getJSON 函数之外的属性。我很困惑。人们不是一直这样做吗?
    • @SlopesideCreative 问题正是我在回答中描述的问题,并且我与 Frescheyball 进行了讨论。当您在 getJSON 之外调用 alert() 时,getJSON 仍在运行并且尚未返回任何数据。所以 yourDATA 仍然是未定义的。
    【解决方案3】:

    我认为这将是您正在寻找的,您正在尝试访问从您的调用返回的数据,而不是调用者对象本身。在您的示例中, jqxhr 是处理 JSON 调用而不是数据的对象。所以,

    $.getJSON("example.json", function(data) {
      yourDATA = data;
    })
    
    //Use your data here
    alert(yourDATA.aProperty);
    

    this page 上的第一个示例与我解释的类似。

    【讨论】:

      猜你喜欢
      • 2016-11-21
      • 2022-01-19
      • 2018-11-01
      • 2018-05-21
      • 2011-04-27
      • 2021-10-24
      • 2018-08-16
      • 1970-01-01
      • 2020-04-12
      相关资源
      最近更新 更多