如何从这个MASSIVE json对象中获取分数?

时间:2016-05-18 00:20:06

标签: php function

我想要$ result ['得分']但是,尽管尝试了$ result->得分,$ result [0]和$ result [stdClass] ['得分'],但它永远不会返回true;非常感谢所有帮助。

Object(stdClass)#1 (9) { ["kind"]=> string(22) "pagespeedonline#result" ["id"]=> string(34) "https://www.google.com/?gws_rd=ssl" ["responseCode"]=> int(200) ["title"]=> string(6) "Google" ["score"]=> int(92) ["pageStats"]=> object(stdClass)#2 (9) { ["numberResources"]=> int(13) ["numberHosts"]=> int(5) ["totalRequestBytes"]=> string(4) "2295" ["numberStaticResources"]=> int(8) ["htmlResponseBytes"]=> string(6) "190230" ["imageResponseBytes"]=> string(5) "45452" ["javascriptResponseBytes"]=> string(6) "780563" ["otherResponseBytes"]=> string(4) "1867" ["numberJsResources"]=> int(4) } ["formattedResults"]=> object(stdClass)#3 (2) { ["locale"]=> string(5) "en_US" ["ruleResults"]=> object(stdClass)#4 (10) { ["AvoidLandingPageRedirects"]=> object(stdClass)#5 (3) { ["localizedRuleName"]=> string(28) "Avoid landing page redirects" ["ruleImpact"]=> float(7) ["urlBlocks"]=> array(2) { [0]=> object(stdClass)#6 (1) { ["header"]=> object(stdClass)#7 (2) { ["format"]=> string(96) "Your page has $1 redirects. Redirects introduce additional delays before the page can be loaded." ["args"]=> array(1) { [0]=> object(stdClass)#8 (2) { ["type"]=> string(11) "INT_LITERAL" ["value"]=> string(1) "2" } } } }

Here is a copy格式化对象的粘贴框

会这样做:

"{\n \"kind\": \"pagespeedonline#result\",\n \"id\": \"https:\/\/www.google.com\/?gws_rd=ssl\",\n \"responseCode\": 200,\n \"title\": \"Google\",\n \"score\": 92,\n \"pageStats\": {\n \"numberResources\": 12,\n \"numberHosts\": 5,\n \"totalRequestBytes\": \"2186\",\n \"numberStaticResources\": 8,\n \"htmlResponseBytes\": \"189695\",\n \"imageResponseBytes\": \"45452\",\n \"javascriptResponseBytes\": \"780569\",\n \"otherResponseBytes\": \"1595\",\n \"numberJsResources\": 4\n },\n \"formattedResults\": {\n \"locale\": \"en_US\",\n \"ruleResults\": {\n \"AvoidLandingPageRedirects\": {\n \"localizedRuleName\": \"Avoid landing page redirects\",\n \"ruleImpact\": 7.0,\n \"urlBlocks\": [\n {\n \"header\": {\n \"format\": \"Your page has $1 redirects. Redirects introduce additional delays before the page can be loaded.\",\n \"args\": [\n {\n \"type\": \"INT_LITERAL\",\n \"value\": \"2\"\n }\n ]\n }\n },\n {\n \"header\": {\n \"format\": \"Avoid landing page redirects for the following chain of redirected URLs.\",\n \"args\": [\n {\n \"type\": \"HYPERLINK\",\n \"value\": \"https:\/\/developers.google.com\/speed\/docs\/insights\/AvoidRedirects\"\n }\n ]\n },\n \"urls\": [\n {\n \"result\": {\n \"format\": \"$1\",\n \"args\": [\n {\n \"type\": \"URL\",\n \"value\": \"http:\/\/google.com\/\"\n }\n ]\n }\n },\n {\n \"result\": {\n \"format\": \"$1\",\n \"args\": [\n {\n \"type\": \"URL\",\n \"value\": \"http:\/\/www.google.com\/\"\n }\n ]\n }\n },\n {\n \"result\": {\n \"format\": \"$1\",\n \"args\": [\n {\n \"type\": \"URL\",\n \"value\": \"https:\/\/www.google.com\/?gws_rd=ssl\"\n }\n ]\n }\n }\n ]\n }\n ]\n },\n \"EnableGzipCompression\": {\n \"localizedRuleName\": \"Enable compression\",\n \"ruleImpact\": 0.0,\n \"urlBlocks\": [\n {\n \"header\": {\n \"format\": \"You have compression enabled. Learn more about enabling compression.\",\n \"args\": [\n {\n \"type\": \"HYPERLINK\",\n \"value\": \"https:\/\/developers.google.com\/speed\/docs\/insights\/EnableCompression\"\n }\n ]\n }\n }\n ]\n },\n \"LeverageBrowserCaching\": {\n \"localizedRuleName\": \"Leverage browser caching\",\n \"ruleImpact\": 0.0,\n \"urlBlocks\": [\n {\n \"header\": {\n \"format\": \"You have enabled browser caching. Learn more about browser caching recommendations.\",\n \"args\": [\n {\n \"type\": \"HYPERLINK\",\n \"value\": \"https:\/\/developers.google.com\/speed\/docs\/insights\/LeverageBrowserCaching\"\n }\n ]\n }\n }\n ]\n },\n \"MainResourceServerResponseTime\": {\n \"localizedRuleName\": \"Reduce server response time\",\n \"ruleImpact\": 0.09,\n \"urlBlocks\": [\n {\n \"header\": {\n \"format\": \"In our test, your server responded in $1. There are many factors that can slow down your server response time. Please read our recommendations to learn how you can monitor and measure where your server is spending the most time.\",\n \"args\": [\n {\n \"type\": \"DURATION\",\n \"value\": \"0.21 seconds\"\n },\n {\n \"type\": \"HYPERLINK\",\n \"value\": \"https:\/\/developers.google.com\/speed\/docs\/insights\/Server\"\n }\n ]\n }\n }\n ]\n },\n \"MinifyCss\": {\n \"localizedRuleName\": \"Minify CSS\",\n \"ruleImpact\": 0.0,\n \"urlBlocks\": [\n {\n \"header\": {\n \"format\": \"Your CSS is minified. Learn more about minifying CSS.\",\n \"args\": [\n {\n \"type\": \"HYPERLINK\",\n \"value\": \"https:\/\/developers.google.com\/speed\/docs\/insights\/MinifyResources\"\n }\n ]\n }\n }\n ]\n },\n \"MinifyHTML\": {\n \"localizedRuleName\": \"Minify HTML\",\n \"ruleImpact\": 0.0,\n \"urlBlocks\": [\n {\n \"header\": {\n \"format\": \"Your HTML is minified. Learn more about minifying HTML.\",\n \"args\": [\n {\n \"type\": \"HYPERLINK\",\n \"value\": \"https:\/\/developers.google.com\/speed\/docs\/insights\/MinifyResources\"\n }\n ]\n }\n }\n ]\n },\n \"MinifyJavaScript\": {\n \"localizedRuleName\": \"Minify JavaScript\",\n \"ruleImpact\": 0.0,\n \"urlBlocks\": [\n {\n \"header\": {\n \"format\": \"Your JavaScript content is minified. Learn more about minifying JavaScript.\",\n \"args\": [\n {\n \"type\": \"HYPERLINK\",\n \"value\": \"https:\/\/developers.google.com\/speed\/docs\/insights\/MinifyResources\"\n }\n ]\n }\n }\n ]\n },\n \"MinimizeRenderBlockingResources\": {\n \"localizedRuleName\": \"Eliminate render-blocking JavaScript and CSS in above-the-fold content\",\n \"ruleImpact\": 0.0,\n \"urlBlocks\": [\n {\n \"header\": {\n \"format\": \"You have no render-blocking resources. Learn more about removing render-blocking resources.\",\n \"args\": [\n {\n \"type\": \"HYPERLINK\",\n \"value\": \"https:\/\/developers.google.com\/speed\/docs\/insights\/BlockingJS\"\n }\n ]\n }\n }\n ]\n },\n \"OptimizeImages\": {\n \"localizedRuleName\": \"Optimize images\",\n \"ruleImpact\": 0.0,\n \"urlBlocks\": [\n {\n \"header\": {\n \"format\": \"Your images are optimized. Learn more about optimizing images.\",\n \"args\": [\n {\n \"type\": \"HYPERLINK\",\n \"value\": \"https:\/\/developers.google.com\/speed\/docs\/insights\/OptimizeImages\"\n }\n ]\n }\n }\n ]\n },\n \"PrioritizeVisibleContent\": {\n \"localizedRuleName\": \"Prioritize visible content\",\n \"ruleImpact\": 0.0,\n \"urlBlocks\": [\n {\n \"header\": {\n \"format\": \"You have the above-the-fold content properly prioritized. Learn more about prioritizing visible content.\",\n \"args\": [\n {\n \"type\": \"HYPERLINK\",\n \"value\": \"https:\/\/developers.google.com\/speed\/docs\/insights\/PrioritizeVisibleContent\"\n }\n ]\n }\n }\n ]\n }\n }\n },\n \"version\": {\n \"major\": 1,\n \"minor\": 15\n },\n \"screenshot\": {\n \"data\": \"_9j_4AAQSkZJRgABAQAAAQABAAD_2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcUFhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSj_2wBDAQcHBwoIChMKChMoGhYaKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCj_wAARCADwAUADASIAAhEBAxEB_8QAHAABAAIDAQEBAAAAAAAAAAAAAAMIAgUGBAEH_8QAQxAAAQMCAgQKCQEGBgMBAAAAAAECAwQRBSEGEhMUFzFBUVJVYZLR0iJTVHGBkZOhogcVMjNCVmIjNHOx4fAllMHx_8QAGgEBAAIDAQAAAAAAAAAAAAAAAAECAwQFBv_EADERAQABAwIE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-i0by_otIQBNvL-i0by_otIQBNvL-i0by_otIQBNvL-i0by_op9yE8tZSJUvjcsj2LHdWqxbZrbP4WA2G8u5mjeX9Fpp20FSx10xCZzURURjky7M0_wC-890DXMhjbI9Xva1Ec5f5ltmoHq3l_RT7jeXczTXVdGs0qSxzPhkRurrMRL2vexhBRTRTRvWunka1VVWPzR2Sp_z8ANpvL-i0by_op9yE1SYVKiNaldM1iLdGtyS9_fdfiBu95dzNG8v6LTTxUNUyojctfK6JllVq8blvmi9hsgP_2Q==\",\n \"height\": 240,\n \"mime_type\": \"image\/jpeg\",\n \"width\": 320\n }\n}\n"

根据请求,这是我将回声更改为echo "Score Speed is: ". print_r(json_decode($result[0]->score, true)) . "<br/>";

所得到的输出

(还有echo "Score Speed is: ". print_r(json_decode($result[0]->score)) . "<br/>";

得分速度为:1

抱歉,这不是更具描述性的。

最终编辑:谢谢大家的帮助......这种组合产生了我的数据:

    $formatted = json_decode($results, JSON_PRETTY_PRINT);

//我使用True而不是JSON_PRETTY_PRINT

来使用旧版本
    echo "formatted data: " . $formatted[0]->score . "<br/>";

//最终无法正常工作

    echo "2nd try: ". $formatted['score'];

//这个可以工作。

...

...

      //This is the [original piece of code I used as a reference point is here][2]
    echo '<pre>';  
    print_r(json_decode($results['score'],true));   
    echo '</pre>'; 

1 个答案:

答案 0 :(得分:2)

你这是错误的方式。您正在尝试解码已解码的值。

只需尝试echo'输出值:

echo $result[0]->score;

这是最后一步。我觉得在此之前的每一步都是错误的 - 导致您无法打印出预期值的问题。

您应该执行以下过程:

$data = FETCH_THE_JSON_FROM_DATA_SOURCE();
$json = json_decode($data);
// now we find data
echo $json[0]->score;

注意:上面的代码是伪代码,您需要适当填写您的部分。

如果我们看不到原始json数据,我们无法告诉您如何获取它。所以,帮助我们帮助你。