代码之家  ›  专栏  ›  技术社区  ›  Jack Bashford

字符串化JSON有不需要的双引号

  •  2
  • Jack Bashford  · 技术社区  · 6 年前

    see here

    我如何才能删除字符串化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>

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

    3 回复  |  直到 6 年前
        1
  •  3
  •   Louys Patrice Bessette    6 年前

    不要把不是物体的东西串起来。就用它们吧。

    $.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>
        2
  •  0
  •   CoolestUsername    6 年前

    改变

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

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

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

        3
  •  0
  •   Ketan Yekale    6 年前

    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");