douyangqian5243
2014-07-06 13:04
浏览 67
已采纳

在PHP中解析Json响应

So I have come to a slight issue in phrasing this api response. I'm trying to phrase the screenshot data from this api response?..

I have tried the below but to no avail!.. It just seems to be NULL?.. any help would be great thanks

$jsonData = json_decode($response, true);
$testt = $jsonData->screenshot->data;

The response:

    {
 "kind": "pagespeedonline#result",
 "id": "http://stackoverflow.com/",
 "responseCode": 200,
 "title": "Stack Overflow",
 "score": 78,
 "pageStats": {
  "numberResources": 31,
  "numberHosts": 11,
  "totalRequestBytes": "4412",
  "numberStaticResources": 22,
 },
 "formattedResults": {
  "locale": "en_US",
  "ruleResults": {
   "AvoidLandingPageRedirects": {
    "localizedRuleName": "Avoid landing page redirects",
    "ruleImpact": 0.0,
    "urlBlocks": [
     {
      "header": {
       "format": "Your page has no redirects. Learn more about avoiding landing page redirects.",
       "args": [
        {
         "type": "HYPERLINK",
         "value": "https://developers.google.com/speed/docs/insights/AvoidRedirects"
        }
       ]
      }
     }
    ]
   },
   "EnableGzipCompression": {
    "localizedRuleName": "Enable compression",
    "ruleImpact": 0.0,
    "urlBlocks": [
     {
      "header": {
       "format": "You have compression enabled. Learn more about enabling compression.",
       "args": [
        {
         "type": "HYPERLINK",
         "value": "https://developers.google.com/speed/docs/insights/EnableCompression"
        }
       ]
      }
     }
    ]
   },
   "LeverageBrowserCaching": {
    "localizedRuleName": "Leverage browser caching",
    "ruleImpact": 0.0,
    "urlBlocks": [
     {
      "header": {
       "format": "You have enabled browser caching. Learn more about browser caching recommendations.",
       "args": [
        {
         "type": "HYPERLINK"
        }
       ]
      }
     }
    ]
   },
   "MainResourceServerResponseTime": {
    "localizedRuleName": "Reduce server response time",
    "ruleImpact": 0.0,
    "urlBlocks": [
     {
      "header": {
       "format": "Your server responded quickly. Learn more about server response time optimization.",
       "args": [
        {
         "type": "HYPERLINK",
         "value": "https://developers.google.com/speed/docs/insights/Server"
        }
       ]
      }
     }
    ]
   },
   "MinifyCss": {
    "localizedRuleName": "Minify CSS",
    "ruleImpact": 0.0,
    "urlBlocks": [
     {
      "header": {
       "format": "Your CSS is minified. Learn more about minifying CSS.",
       "args": [
        {
         "value": "https://developers.google.com/speed/docs/insights/MinifyResources"
        }
       ]
      }
     }
    ]
   },
   "MinifyHTML": {
    "ruleImpact": 0.1443,
    "urlBlocks": [
     {
      "header": {
       "format": "Compacting HTML code, including any inline JavaScript and CSS contained in it, can save many bytes of data and speed up download and parse times."
      }
     },
     {
      "header": {
       "format": "Minify HTML for the following resources to reduce their size by $2 ($3 reduction).",
       "args": [
        {
         "value": "https://developers.google.com/speed/docs/insights/MinifyResources"
        },
        {
         "value": "1.4KiB"
        },
        {
         "value": "5%"
        }
       ]
      },
      "urls": [
       {
        "result": {
         "format": "Minifying $1 could save $2 ($3 reduction) after compression.",
         "args": [
          {
           "type": "URL",
           "value": "http://stackoverflow.com/"
          },
          {
           "type": "BYTES",
           "value": "1.4KiB"
          },
          {
           "value": "5%"
          }
         ]
        }
       }
      ]
     }
    ]
   },
   "MinifyJavaScript": {
    "localizedRuleName": "Minify JavaScript",
    "ruleImpact": 0.0,
    "urlBlocks": [
     {
      "header": {
       "format": "Your JavaScript content is minified. Learn more about minifying JavaScript.",
       "args": [
        {
         "type": "HYPERLINK",
         "value": "https://developers.google.com/speed/docs/insights/MinifyResources"
        }
       ]
      }
     }
    ]
   },
   "MinimizeRenderBlockingResources": {
    "localizedRuleName": "Eliminate render-blocking JavaScript and CSS in above-the-fold content",
    "urlBlocks": [
     {
      "header": {
       "args": [
        {
         "type": "INT_LITERAL",
         "value": "2"
        },
        {
         "type": "INT_LITERAL",
         "value": "3"
        }
       ]
      }
     },
     {
      "header": {
       "format": "None of the above-the-fold content on your page could be rendered without waiting for the following resources to load. Try to defer or asynchronously load blocking resources, or inline the critical portions of those resources directly in the HTML."
      }
     },
     {
      "header": {
       "args": [
        {
         "type": "HYPERLINK",
         "value": "https://developers.google.com/speed/docs/insights/BlockingJS"
        }
       ]
      },
      "urls": [
       {
        "result": {
         "format": "$1",
         "args": [
          {
           "type": "URL",
           "value": "http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"
          }
         ]
        }
       },
       {
        "result": {
         "format": "$1",
         "args": [
          {
           "type": "URL",
           "value": "http://cdn.sstatic.net/Js/stub.en.js?v=174f553de1d0"
          }
         ]
        }
       }
      ]
     },
     {
      "header": {
       "format": "Optimize CSS Delivery of the following:",
       "args": [
        {
         "type": "HYPERLINK",
         "value": "https://developers.google.com/speed/docs/insights/OptimizeCSSDelivery"
        }
       ]
      },
      "urls": [
       {
        "result": {
         "format": "$1",
         "args": [
          {
           "type": "URL",
           "value": "http://cdn.sstatic.net/stackoverflow/all.css?v=55bbe6171ff2"
          }
         ]
        }
       },
       {
        "result": {
         "format": "$1",
         "args": [
          {
           "type": "URL",
           "value": "http://cdn-careers.sstatic.net/careers/gethired/company.min.css?v=86ee8c8bfa3e"
          }
         ]
        }
       },
       {
        "result": {
         "format": "$1",
         "args": [
          {
           "type": "URL",
           "value": "http://cdn-careers.sstatic.net/careers/gethired/sidebar.min.css?v=88f3fd5e2263"
          }
         ]
        }
       }
      ]
     }
    ]
   },
   "OptimizeImages": {
    "localizedRuleName": "Optimize images",
    "ruleImpact": 3.0802000000000005,
    "urlBlocks": [
     {
      "header": {
       "format": "Properly formatting and compressing images can save many bytes of data."
      }
     },
     {
      "header": {
       "format": "Optimize the following images to reduce their size by $2 ($3 reduction).",
       "args": [
        {
         "type": "HYPERLINK",
         "value": "https://developers.google.com/speed/docs/insights/OptimizeImages"
        },
        {
         "type": "BYTES",
         "value": "30KiB"
        },
        {
         "type": "PERCENTAGE",
         "value": "42%"
        }
       ]
      },
      "urls": [
       {
        "result": {
         "format": "Losslessly compressing $1 could save $2 ($3 reduction).",
         "args": [
          {
           "type": "URL",
           "value": "http://cdn-careers.sstatic.net/careers/gethired/img/careers4-ad-header-so-crop.png?v=f4ca4345dc81"
          },
          {
           "type": "BYTES",
           "value": "16.6KiB"
          },
          {
           "type": "PERCENTAGE",
           "value": "73%"
          }
         ]
        }
       },
       {
        "result": {
         "format": "Losslessly compressing $1 could save $2 ($3 reduction).",
         "args": [
          {
           "type": "URL",
           "value": "http://cdn.sstatic.net/img/share-sprite-new.png?v=204b1e0e421b"
          },
          {
           "type": "BYTES",
           "value": "2.9KiB"
          },
          {
           "type": "PERCENTAGE",
           "value": "19%"
          }
         ]
        }
       },
       {
        "result": {
         "format": "Losslessly compressing $1 could save $2 ($3 reduction).",
         "args": [
          {
           "type": "URL",
           "value": "http://cdn.sstatic.net/Img/sprite-herobox.png?v=d173774f3a9f"
          },
          {
           "type": "BYTES",
           "value": "2.8KiB"
          },
          {
           "type": "PERCENTAGE",
           "value": "43%"
          }
         ]
        }
       },
       {
        "result": {
         "format": "Losslessly compressing $1 could save $2 ($3 reduction).",
         "args": [
          {
           "type": "URL",
           "value": "http://i.stack.imgur.com/Tfe0K.png"
          },
          {
           "type": "BYTES",
           "value": "2.6KiB"
          },
          {
           "type": "PERCENTAGE",
           "value": "76%"
          }
         ]
        }
       },
       {
        "result": {
         "format": "Losslessly compressing $1 could save $2 ($3 reduction).",
         "args": [
          {
           "type": "URL",
           "value": "http://i.stack.imgur.com/vobok.png"
          },
          {
           "type": "BYTES",
           "value": "2.6KiB"
          },
          {
           "type": "PERCENTAGE",
           "value": "76%"
          }
         ]
        }
       },
       {
        "result": {
         "format": "Losslessly compressing $1 could save $2 ($3 reduction).",
         "args": [
          {
           "type": "URL",
           "value": "http://cdn.sstatic.net/stackoverflow/img/sprites.png?v=3c6263c3453b"
          },
          {
           "type": "BYTES",
           "value": "1.5KiB"
          },
          {
           "type": "PERCENTAGE",
           "value": "9%"
          }
         ]
        }
       },
       {
        "result": {
         "format": "Losslessly compressing $1 could save $2 ($3 reduction).",
         "args": [
          {
           "type": "URL",
           "value": "http://i.stack.imgur.com/G1dzB.png"
          },
          {
           "type": "BYTES",
           "value": "962B"
          },
          {
           "type": "PERCENTAGE",
           "value": "50%"
          }
         ]
        }
       }
      ]
     }
    ]
   },
   "PrioritizeVisibleContent": {
    "localizedRuleName": "Prioritize visible content",
    "ruleImpact": 2.0,
    "urlBlocks": [
     {
      "header": {
       "format": "Your page requires additional network round trips to render the above-the-fold content. For best performance, reduce the amount of HTML needed to render above-the-fold content."
      }
     },
     {
      "header": {
       "format": "The entire HTML response was not sufficient to render the above-the-fold content. This usually indicates that additional resources, loaded after HTML parsing, were required to render above-the-fold content. Prioritize visible content that is needed for rendering above-the-fold by including it directly in the HTML response.",
       "args": [
        {
         "type": "HYPERLINK",
         "value": "https://developers.google.com/speed/docs/insights/PrioritizeVisibleContent"
        }
       ]
      },
      "urls": [
       {
        "result": {
         "format": "Only about $1 of the final above-the-fold content could be rendered with the full HTML response $2.",
         "args": [
          {
           "type": "PERCENTAGE",
           "value": "26%"
          },
          {
           "type": "SNAPSHOT_RECT",
           "value": "snapshot:5"
          }
         ]
        }
       }
      ]
     }
    ]
   }
  }
 },
 "version": {
  "major": 1,
  "minor": 15
 },
 "screenshot": {
  "data": "***************8This is the data i need!!*********",
  "height": 240,
  "mime_type": "image/jpeg",
  "width": 320
 }
}
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • doubu7134 2014-07-06 13:09
    已采纳

    You set the assoc Parameter in json_decode on true, so it will return an array! You just have to delete true like this:

    $jsonData = json_decode($response);
    $testt = $jsonData->screenshot->data;
    

    Of course you can change true to false aswell:

    $jsonData = json_decode($response, false);
    $testt = $jsonData->screenshot->data;
    

    More information about this in the PHP-Manual for json_decode

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongshen5115 2014-07-06 13:13

    use it like

    $jsonData = json_decode($aa, true);
    echo $jsonData['screenshot']['data'];
    

    working code

    评论
    解决 无用
    打赏 举报
  • duanliang789262 2014-07-06 13:15

    First, there is an error in your JSON on line 11 that stops json_deocde() returning a valid array (you should remove the trailing comma):

    "numberStaticResources": 22,
    

    Second, you are getting an array from json_decode() so you need to use the correct syntax:

    $jsonData = json_decode($response, true);
    $testt = $jsonData['screenshot']['data'];
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题