简体   繁体   English

如何将Google PageSpeed Insight JSON结果转换为HTML

[英]How to convert Google PageSpeed Insight JSON results to HTML

I have never worked with JSON before and I am stuck at converting my results to html. 我以前从未使用过JSON,并且一直坚持将结果转换为html。 I would like them to spit out as ul's and li's preferably. 我希望他们最好像ul和li一样吐出来。 I have tried plugins and Jquery scripts but nothing seems to work. 我尝试了插件和Jquery脚本,但似乎没有任何效果。 My assumption is that the way I am spitting out the results is incorrect, but as I said I have no idea what I am doing with server response objects. 我的假设是,我吐出结果的方式是不正确的,但是正如我所说的,我不知道我在处理服务器响应对象。

HTML: HTML:

<input type="text" placeholder="Enter webpage URL e.g.http://www.domain.com" id="url"/>
<input type="button" id="button" value="PageSpeed Data" onclick="clicked();" />
<div id="urlerror">Please Enter a Valid URL e.g. http://www.domain.com</div>
<pre id="data"></pre>

My code to get the results: 我的代码获得结果:

<script>
function clicked()
{   
    document.getElementById("urlerror").style.display = 'none'; 
    var xhr = new XMLHttpRequest();
    var url = document.getElementById("url").value;
    if(url.indexOf('http://') === -1){document.getElementById("urlerror").style.display = 'block'; return;}
    var xhr =  new XMLHttpRequest();
    xhr.open("GET", "https://www.googleapis.com/pagespeedonline/v1/runPagespeed?url="+encodeURIComponent(url)+"&key=AIzaSyAIOUFcLYeo2WN1qbPSjlMbXmLi8kmOacw&strategy=mobile");
    xhr.onload = function(){
    document.getElementById("data").innerHTML = xhr.responseText;
}
xhr.send();
}
</script>

The resulting short snippet JSON object example (not full code): 生成的简短代码段JSON对象示例(不完整的代码):

{
 "kind": "pagespeedonline#result",
 "id": "http://www.celebritynewsdaily.us/",
 "responseCode": 200,
 "title": "Celebrity News Daily | Your Daily Source for Celebrity News & Updates",
 "score": 64,
 "pageStats": {
  "numberResources": 71,
  "numberHosts": 13,
  "totalRequestBytes": "11777",
  "numberStaticResources": 35,
  "htmlResponseBytes": "235467",
  "textResponseBytes": "238",
  "cssResponseBytes": "135950",
  "imageResponseBytes": "545748",
  "javascriptResponseBytes": "762058",
  "otherResponseBytes": "107518",
  "numberJsResources": 13,
  "numberCssResources": 11
 },
  "formattedResults": {
  "locale": "en_US",
  "ruleResults": {
  "AvoidInterstitials": {
  "localizedRuleName": "Avoid app install interstitials that hide content",
  "ruleImpact": 0.0,
  "urlBlocks": [
  {
  "header": {
  "format": "Your page does not appear to have any app install interstitials that hide a significant amount of content. Learn more about the importance of avoiding the use of app install interstitials.",
  "args": [
    {
     "type": "HYPERLINK",
     "value": "https://developers.google.com/webmasters/mobile-sites/mobile-seo/common-mistakes/avoid-interstitials"
      }
     ]
    }
   }
  ]
 }
}

Any help getting this working is greatly appreciated. 非常感谢您对此工作的任何帮助。

Once you've got the JSON string, it is easy to convert into a JavaScript object by calling JSON.parse() . 获得JSON字符串后,可以很容易地通过调用JSON.parse()转换为JavaScript对象。

var myObject = JSON.parse(jsonString);

Once you've got the object, you can reference values within it as per a normal JS object; 一旦有了对象,就可以按照普通的JS对象引用其中的值。 eg myObject.pageStats.numberResources , and use the DOM methods to insert them into DOM elements. 例如myObject.pageStats.numberResources ,并使用DOM方法将其插入DOM元素。

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

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