在HTML页面上将URL解析为可读的json格式

Parse a URL into readable json format on a HTML page?

本文关键字:格式 json HTML URL      更新时间:2023-09-26

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

如果您确实需要将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>

您可以在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);
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>

Per将对象转换为字符串

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对象变量中,由您自己处理。。。