【问题标题】:Javascript not reading json fileJavascript不读取json文件
【发布时间】:2019-09-14 14:52:05
【问题描述】:

我正在制作基于 html 标签的播放器,需要播放列表才能播放。我在 Raspberry Pi 3 上读取 chromium 上的 json 文件(本地文件)时遇到问题。整个事情已经在我的笔记本电脑上使用 EDGE 运行(getJSON 在代码后面的代码中适用于 Raspberry)。我得到的错误是 statusText: parser error。

我已经尝试过 ajax 和所有组合,例如:读取文本然后将其解析为 json 对象,我使用了 ajax 参数(也尝试过 jsonp)。有趣的是,相同的功能在 10 秒内就可以毫无问题地运行到代码中。验证器也检查了两个 json 文件并且没问题。

顺便说一句,我有一个 python 脚本,它使用 --allow-all-files 在 chromium 中运行所有这些...

//All variables are defined as var globally

//READ PLAYLIST
//This is the function that returns error (it is called by body -> onload)

  function read_playlist() {
    $.getJSON("playlist.json", function (data) {
      music = data;
      cnt = data.length;
    });    
  }


//READ SLIDESHOW
//This is the same function without error (is called by setTimeout(this, 1000))

  $.getJSON("./slideshow.json", function (data1) {
    imgs = data1;
    i_cnt = data1.length;
    i_index = data1.length;
  });

这里是保存为playlist.json的json

[{"title":"George Ezra - Paradise","download":"music\/2018 Weekly Charts\/George Ezra - Paradise.mp3","file":"songs\/George Ezra - Paradise.mp3","poster":"images\/logo.png","mp3":"songs\/George Ezra - Paradise.mp3"},{"title":"Jax Jones - Breathe (feat. Ina Wroldsen)_N","download":"music\/2018 Weekly Charts\/Jax Jones - Breathe (feat. Ina Wroldsen)_N.mp3","file":"songs\/Jax Jones - Breathe (feat. Ina Wroldsen)_N.mp3","poster":"images\/logo.png","mp3":"songs\/Jax Jones - Breathe (feat. Ina Wroldsen)_N.mp3"}]

和幻灯片.json

["logo.png", "l.jpg"]

【问题讨论】:

  • 使用"./playlist.json" 作为路径。虽然那个 JSON 不好,因为有效的 JSON 总是以大括号开头和结尾......在最好的情况下它可能是可以容忍的。
  • 你的函数没有返回任何东西。还不清楚musiccnt 是什么。这些是全局变量吗?
  • @MartinZeitler 因为有效的 JSON 总是以大括号开头和结尾这只是 错误。 OP 的 JSON 完全有效。
  • @MartinZeitler 请注意,包含以下任何文字的文件也是有效的 JSON:"foo"true1337""null。跨度>
  • @MartinZeitler JSON 值可以是对象、数组、数字、字符串、true、false 或 null。 ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf

标签: javascript jquery json ajax chromium


【解决方案1】:

好的,所以问题不在脚本中,而是在 json 中。我从我的笔记本电脑上发布了 json,它是从树莓下载它的同一个链接下载的。但是那个 python 文件以某种方式将 json "hm" 转换为 'hm' 所以它不再有效(因为 'hm' 不是字符串)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-26
    • 2011-07-05
    • 2013-05-15
    • 2014-09-09
    • 2013-03-20
    相关资源
    最近更新 更多