【问题标题】:Parse a URL into readable json format on a HTML page? [closed]在 HTML 页面上将 URL 解析为可读的 json 格式? [关闭]
【发布时间】:2015-06-11 11:01:06
【问题描述】:

我想知道如何在 html 页面上使用 jQuery 将此 URL 解析为可读的 JSON 格式。

【问题讨论】:

  • 网上有很多关于如何做到这一点的教程。请展示您尝试过的内容。 SO 并不打算只是转储一堆数据并取回代码。你应该自己做基础研究,当你在发布实时代码时遇到问题
  • 什么网址?请在问题本身中包含所有相关信息和代码。

标签: javascript jquery html json parsing


【解决方案1】:

如果确实需要将json解析成html,请使用JSON.stringify()

$.getJSON( "http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=Cher&api_key=a0b2a6ee7f4de028004b9ce7e4a29f42&format=json", function(data) {
    $("div").text(JSON.stringify(data))
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div></div>

【讨论】:

    【解决方案2】:

    Converting an object to a string

    var o = {a:1, b:2};
    console.log(o);
    console.log('Item: ' + o);
    console.log('Item: ', o);  
    

    输出:

    Object { a=1, b=2}           // useful
    Item: [object Object]        // not useful
    Item:  Object {a: 1, b: 2}   // Best of both worlds! :)
    

    那么这个家庭作业是针对哪个班级的呢?你的具体任务是什么?提示:我们将让您自己从提要中获取数据并将其存储到 javascript 对象变量中...

    【讨论】:

      【解决方案3】:

      您可以在 jQuery 中使用 $.getJSON() 函数。这会将对象写入控制台。它是人类可读的。您可以使用 javascript 进行任何操作。

      $.getJSON( "http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=Cher&api_key=a0b2a6ee7f4de028004b9ce7e4a29f42&format=json", function(data) {
          console.log(data);
        });
      &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"&gt;&lt;/script&gt;

      【讨论】:

        猜你喜欢
        • 2019-07-26
        • 1970-01-01
        • 2011-07-06
        • 1970-01-01
        • 2018-02-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多