【问题标题】:Phrasing Json response in PHP在 PHP 中措辞 Json 响应
【发布时间】:2023-04-05 02:37:01
【问题描述】:

所以我在措辞这个 api 响应时遇到了一个小问题。 我正在尝试从这个 api 响应中提取屏幕截图数据?..

我尝试了以下但无济于事!..它似乎是NULL?.. 任何帮助都会非常感谢

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

回应:

    {
 "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
 }
}

【问题讨论】:

  • 在 json_decode 中设置 true 使数据关联数组

标签: php jquery mysql json response


【解决方案1】:

您在true 上设置json_decode 中的assoc 参数,因此它将返回一个数组! 你只需要像这样删除true

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

当然你也可以把true改成false

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

PHP-Manual for json_decode

中的更多信息

【讨论】:

    【解决方案2】:

    像这样使用它

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

    working code

    【讨论】:

      【解决方案3】:

      首先,第 11 行的 JSON 中有一个错误,导致 json_deocde() 返回有效数组(您应该删除结尾的逗号):

      "numberStaticResources": 22,
      

      其次,你从 json_decode() 得到一个数组,所以你需要使用正确的语法:

      $jsonData = json_decode($response, true);
      $testt = $jsonData['screenshot']['data'];
      

      【讨论】:

        猜你喜欢
        • 2015-03-18
        • 2019-11-02
        • 2020-02-17
        • 1970-01-01
        • 1970-01-01
        • 2015-07-30
        • 1970-01-01
        • 2018-10-14
        • 2021-01-22
        相关资源
        最近更新 更多