简体   繁体   English

使用.getJSON而不使用嵌入式数据时,控制台中出现未定义的JSON错误

[英]Undefined JSON error in console when using .getJSON but not using embedded data

Could anyone help explain to me why my console.log(tooltipValues is coming back as undefined when the console.log(tooltipJSON) is populating no problem? 谁能帮助我解释一下当console.log(tooltipJSON)填充没有问题时,为什么我的console.log(tooltipValues )返回未定义状态?

I'm not entirely sure why, but when I just place the data inside of my JS, it works just fine. 我不确定为什么,但是当我将数据放入JS时,它就可以正常工作。 But as soon as I export the data to a JSON file it breaks. 但是,一旦我将数据导出到JSON文件,它就会中断。 To be clear, I can console.log(tooltipJSON) no problem when it's an external JSON file. 明确地说,当它是一个外部JSON文件时,我可以console.log(tooltipJSON)没问题。 Super stuck on this as I can't have embedded JSON due to it longterm holding 1000's of records. 超级卡住了这一点,因为我无法嵌入JSON,因为它可以长期保存1000条记录。

var tooltipJSON;
    $.getJSON("js/tooltips.json", function (data) {
      tooltipJSON = data;

      $('.skill, .trinket, .hero').hover(
        function() {
          var tooltipValues = tooltipJSON[$(this).data("tooltip")];    
          console.log(tooltipValues);
          console.log(tooltipJSON);

          if(!tooltipValues)return;
          var tooltip = $("<div class='tp-popup'><div>" + tooltipValues.value1 + "</div><div>" + tooltipValues.value2 + "</div></div>")
            .css({
              'color': '#fff',
              'position': 'absolute',
              'zIndex': '99999',
              'width': '100px',
              'height': '150px',
              'background-color': '#333',
            });
          $(this).append(tooltip);
          $(document).on('mousemove', function(e) {
            $('.tp-popup').css({
              left: e.pageX,
              top: e.pageY,
            });
          });
        },
        function() {
          $('.tp-popup').remove();
        }
      ); 
    });

EXTERNAL JSON USED IN THE ABOVE: 上面使用的外部JSON:

[
  {
    "skill-one": {
        "value1": "skill-one value1",
        "value2": "skill-one value2",
        "value3": "skill-one value3"
    },
    "trinket-two": {
        "value1": "trinket-two value1",
        "value2": "trinket-two value2",
        "value3": "trinket-two value3"
    }
  }
]

This returns with no issues though? 这返回没有问题吗?

tooltipJSON = {
    "skill-one": {
      "value1": "skill-one value1",
      "value2": "skill-one value2",
      "value3": "skill-one value3"
    },
    "trinket-two": {
      "value1": "trinket-two value1",
      "value2": "trinket-two value2",
      "value3": "trinket-two value3"
    },
}
$('.skill, .trinket, .hero').hover(
        function() {
          var tooltipValues = tooltipJSON[$(this).data("tooltip")];    
          console.log(tooltipValues);
          console.log(tooltipJSON);

          if(!tooltipValues)return;
          var tooltip = $("<div class='tp-popup'><div>" + tooltipValues.value1 + "</div><div>" + tooltipValues.value2 + "</div></div>")
            .css({
              'color': '#fff',
              'position': 'absolute',
              'zIndex': '99999',
              'width': '100px',
              'height': '150px',
              'background-color': '#333',
            });
          $(this).append(tooltip);
          $(document).on('mousemove', function(e) {
            $('.tp-popup').css({
              left: e.pageX,
              top: e.pageY,
            });
          });
        },
        function() {
          $('.tp-popup').remove();
        }
      ); 

This is the code to focus on, for whatever reason console.log(tooltipValues); 无论出于何种原因,这都是要关注的代码console.log(tooltipValues); doesn't work with the external JSON, but the console log of the JSON itself works and populates no issues. 不适用于外部JSON,但是JSON的控制台日志本身可以工作,并且不会出现任何问题。 Note that both consoles.log's work with embedded internal JSON. 请注意,两个consoles.log均使用嵌入式内部JSON。

$('.skill, .trinket, .hero').hover(
        function() {
          var tooltipValues = tooltipJSON[$(this).data("tooltip")];    
          console.log(tooltipValues);
          console.log(tooltipJSON);

JQuery's getJSON method works differently across different JQuery versions, giving silent errors unless you implement the fail callback. JQuery的getJSON方法在不同的JQuery版本中的工作方式不同,除非实现了fail回调,否则会给出静默错误。 You have a trailing comma before the end of your JSON content that is causing the method to fail. JSON内容末尾带有comma ,导致方法失败。 Also, I was using JQuery 2.1 and the method did not show anything. 另外,我使用的是JQuery 2.1,该方法未显示任何内容。 I upgraded to version 3.1.1 and then traced and fixed the error. 我升级到版本3.1.1 ,然后跟踪并修复了该错误。 Below is the relevant code of my test. 以下是我测试的相关代码。

var tooltipJSON;
$.getJSON("js/tooltips.json", function (data) {
  tooltipJSON = data;
  console.log("data", data);
  var key = $(this).data("tooltip"), tooltipValues = tooltipJSON[key];
  console.log("'"+key+"'", tooltipValues);
}).fail(function (err) {
  console.log("error", err);
});

The external JSON should be an object {...} rather than an array containing an object [{...}] : 外部JSON应该是对象{...}而不是包含对象[{...}]的数组:

{
  "skill-one": {
    "value1": "skill-one value1",
    "value2": "skill-one value2",
    "value3": "skill-one value3"
  },
  "trinket-two": {
    "value1": "trinket-two value1",
    "value2": "trinket-two value2",
    "value3": "trinket-two value3"
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM