繁体   English   中英

如何在Javascript中使用JSON数据

[英]How to use JSON data with Javascript

我是javascript的新手,因此真的不知道如何问我要问的问题。 这样做的目的是要道歉,如果这是一个重复的问题。 话虽如此,我偶然发现了该站点,并希望利用此处讨论的方法。 鉴于我对此类工具的使用案例将涉及使用Python或R动态生成JSON,我想知道如何

a)适当地生成JSON。

b)将其与<script>标记集成在一起,使其成为Javascipt中的JSON对象。

这是我现在拥有的代码:

html <- paste('<head><link title="timeline-styles", rel="stylesheet" href="//cdn.knightlab.com/libs/timeline3/latest/css/timeline.css"><script src="//cdn.knightlab.com/libs/timeline3/latest/js/timeline.js"></script></head><body><div id="timeline-embed" style="width: 100%; height: 600px"></div><script type="text/javascript">var timeline_json=', readr::read_lines("~/projects/timelineJS/trial.json") %>% paste(collapse=''),'; window.timeline=new TL.Timeline("timeline-embed", timeline_json);</script></body>', sep='')
write(html, file="~/projects/timelineJS/test.html")

输出似乎是我想要的(下面的输出已清理):

<head>
  <link title="timeline-styles" rel="stylesheet" href="//cdn.knightlab.com/libs/timeline3/latest/css/timeline.css">
  <script src="//cdn.knightlab.com/libs/timeline3/latest/js/timeline.js"></script>
</head>
<body>
  <div id="timeline-embed" style="width: 100%; height: 600px"></div>
  <script type="text/javascript">
    var timeline_json={"title": {"media": {"url": "//www.flickr.com/photos/tm_10001/2310475988/", "caption": "Whitney Houston performing on her My Love is Your Love Tour in Hamburg.", "credit": "flickr/<a href='http://www.flickr.com/photos/tm_10001/'>tm_10001</a>"}, "text": {"headline": "Whitney Houston<br/> 1963 - 2012", "text": "<p>Houston's voice caught the imagination of the world propelling her to superstardom at an early age becoming one of the most awarded performers of our time. This is a look into the amazing heights she achieved and her personal struggles with substance abuse and a tumultuous marriage.</p>"}}, "events": ["media": {"url": "https://youtu.be/fSrO91XO1Ck", "caption": "", "credit": "<a href=\"http://unidiscmusic.com\">Unidisc Music</a>"}, "start_date": {"year": "1978"}, "text": {"headline": "First Recording", "text": "At the age of 15 Houston was featured on Michael Zager's song, Life's a Party."}]};
    window.timeline = new TL.Timeline("timeline-embed", timeline_json);
  </script>
</body>

但是,当我加载html文件时,它只是一个空白屏幕。 我不知道自己在做什么方面做得很好,无法知道从哪里开始调试,因此朝着正确方向提供的任何帮助将不胜感激。 谢谢!

事件必须是对象数组。

普通数组将仅包含一个值,即Days = [ "mon", "tues" .... }

一个对象可以保存多条信息(甚至是功能)。

您需要告诉JavaScript您要使用array [],并且此数组包含多个对象{},所以最终得到[{},{},{},{}]

使用您之前的代码,您告诉JSON解析器期望一个数组。 解析器查找直到:的值。 由于这不是数组定界符,因此会导致解析器抛出错误

"events": [
  "media": {
     "url": "https://youtu.be/fSrO91XO1Ck", 
     "caption": "", 
     "credit": "<a href=\"http://unidiscmusic.com\">Unidisc Music</a>"
  }, 
  "start_date": {"year": "1978"}, 
  "text": {
     "headline": "First Recording", 
     "text": "At the age of 15 Houston was featured on Michael Zager's song, Life's a Party."
  }
]

此代码告诉解析期望一个数组[下一个符号用于对象。 因此,解析器将期望包含一个或多个对象的数组。

"events": [{
  "media": {
     "url": "https://youtu.be/fSrO91XO1Ck", 
     "caption": "", 
     "credit": "<a href=\"http://unidiscmusic.com\">Unidisc Music</a>"
  }, 
  "start_date": {"year": "1978"}, 
  "text": {
     "headline": "First Recording", 
     "text": "At the age of 15 Houston was featured on Michael Zager's song, Life's a Party."
  }
}]

解析器如果非常挑剔。 如果发现错误,它将停止处理数据。 确保查看浏览器控制台日志(F12和控制台-在Chrome中)。

暂无
暂无

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

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