在Javascript中解析头的JSON响应

时间:2015-01-12 07:14:23

标签: javascript jquery json phantomjs

我在这里有一个JSON响应http://pastebin.com/Z24RpW5N。我想解析它为X-Akamai-FEO-State:的值,然后根据值调用一些方法来做某事。

我有PhantomJS的以下JS文件。

page.onResourceReceived = function(response) {
console.log(JSON.stringify(response.headers));
//here check for Akamai-FEO-State: and if its Transforming then call 

};

如何在JS中解析此JSON以获取标头的值。

1 个答案:

答案 0 :(得分:1)

var arr = [{"name":"Accept-Ranges","value":"bytes"},{"name":"Content-Type","value":"text/html; charset=UTF-8"},{"name":"ETag","value":"\"1e01b5-da04-50c67bfe03b6f\""},{"name":"Last-Modified","value":"Sun, 11 Jan 2015 22:17:03 GMT"},{"name":"Server","value":"Apache"},{"name":"X-Akamai-Transformed","value":"c - 0 -"},{"name":"Content-Encoding","value":"gzip"},{"name":"Cache-Control","value":"max-age=14400"},{"name":"Expires","value":"Mon, 12 Jan 2015 11:09:40 GMT"},{"name":"Date","value":"Mon, 12 Jan 2015 07:09:40 GMT"},{"name":"Connection","value":"keep-alive"},{"name":"X-Akamai-FEO-Host-For-Transformation-Fetch","value":"r17016.feo-prod.akamai.com"},{"name":"X-Akamai-FEO-Transformation-Region","value":"17016"},{"name":"X-Akamai-FEO-Config-Region","value":"17810"},{"name":"X-Akamai-FEO-Config-Release-Label","value":"release-feo-6755.1.1"},{"name":"X-Akamai-FEO-Transformation-SoftwareVersion","value":"675511"},{"name":"X-Akamai-FEO-Config-SoftwareVersion","value":"675511"},{"name":"X-Akamai-FEO-Transformations-Expires","value":"Mon, 12 Jan 2015 07:10:45 GMT"},{"name":"X-Akamai-FEO-Transformations-Last-Modified","value":"Sun, 11 Jan 2015 21:41:59 GMT"},{"name":"X-Akamai-FEO-transformations-Version","value":"1418449376"},{"name":"X-Akamai-FEO-Config-Expires","value":"Mon, 12 Jan 2015 07:10:06 GMT"},{"name":"X-Akamai-FEO-Config-Last-Modified","value":"Fri, 05 Dec 2014 12:04:54 GMT"},{"name":"X-Akamai-FEO-Config-Version","value":"1418449376"},{"name":"X-Akamai-FEO-Browser-Type","value":"Chrome"},{"name":"X-Akamai-FEO-Transformations-Key","value":"user=ARL_www.ubank.com.au.145216.xml&normurl=3260495&release=release-feo-6755.1.1"},{"name":"X-Akamai-FEO-State","value":"TRANSFORMING"},{"name":"Vary","value":"User-Agent"}];

for(var key in arr)
{
   if(arr[key].name === 'X-Akamai-FEO-State')
   {
      console.log(arr[key].value); // TRANSFORMING
   }
}