Stringified JSON 有不需要的双引号

Stringified JSON has unwanted double quotes

我正在制作一个天气预报网站,并查询 API returns 纯 JSON 响应。但是,当我将数据字符串化并将其附加到一个元素时,将其放在网页上时,它有双引号,请参见此处:

我怎样才能从字符串化的 JSON 文本中删除双引号,以便它像普通网页一样显示?我的代码如下:

$.get("https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22canberra%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys",
  function(data) {
    $("body")
      .append("Count: " + JSON.stringify(data.query.count)); 
    $("#heading")  
      .append(JSON.stringify(data.query.results.channel.title));
  },
"json");
<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Yahoo Weather for Canberra</title>
    <meta charset="utf-8">
    <script src="https://code.jquery.com/jquery-3.3.1.js"></script>
  </head>
  <body>
    <h1 id="heading"></h1>
  </body>
</html>

所有帮助将不胜感激。谢谢

不要将不是对象的东西字符串化。就用它们吧。

$.get("https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22canberra%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys",
function(data) {
  $(document.body).append("Count: " + data.query.count); 
  $("#heading").append(data.query.results.channel.title);
}, "json");
<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Yahoo Weather for Canberra</title>
    <meta charset="utf-8">
    <script src="https://code.jquery.com/jquery-3.3.1.js"></script>
  </head>
  <body>
    <h1 id="heading"></h1>
  </body>
</html>

改变

.append(JSON.stringify(data.query.results.channel.title));

.append(data.query.results.channel.title);

因为data.query.results.channel.title已经是字符串了。

console.log(typeof data.query.results.channel.title); // 字符串

JSON.stringify 提供 JSON 对象的字符串表示。

您不需要使用原始数据类型对值进行字符串化。

所以您应该使用的代码是:

$.get("https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22canberra%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys", function(data) {
     $("body").append("Count: " + data.query.count); //  2pm
     $("#heading").append( data.query.results.channel.title);
}, "json");