如何在PHP中解码JSON


how to decode a json in php

这是我的json,

{
  "responseHeader":{
    "status":0,
    "QTime":15},
  "response":{"numFound":0,"start":0,"docs":[]
  },
  "spellcheck":{
    "suggestions":[
      "abert",{
        "numFound":10,
        "startOffset":0,
        "endOffset":5,
        "origFreq":0,
        "suggestion":[{
            "word":"albert",
            "freq":126},
          {
            "word":"aber t",
            "freq":317},
          {
            "word":"alert",
            "freq":58},
          {
            "word":"a bert",
            "freq":13},
          {
            "word":"abort",
            "freq":57},
          {
            "word":"a be rt",
            "freq":1045},
          {
            "word":"avert",
            "freq":37},
          {
            "word":"ab e rt",
            "freq":335},
          {
            "word":"aberr",
            "freq":21},
          {
            "word":"ab er t",
            "freq":317}]},
      "enstin",{
        "numFound":10,
        "startOffset":6,
        "endOffset":12,
        "origFreq":0,
        "suggestion":[{
            "word":"einstein",
            "freq":92},
          {
            "word":"ens tin",
            "freq":44},
          {
            "word":"enshrin",
            "freq":13},
          {
            "word":"en s tin",
            "freq":673},
          {
            "word":"enjoin",
            "freq":12},
          {
            "word":"e ns tin",
            "freq":335},
          {
            "word":"entwin",
            "freq":8},
          {
            "word":"ens t in",
            "freq":317},
          {
            "word":"epstein",
            "freq":8},
          {
            "word":"en st in",
            "freq":231}]},
      "correctlySpelled",false,
      "collation",[
        "collationQuery","albert einstein",
        "hits",154,
        "misspellingsAndCorrections",[
          "abert","albert",
          "enstin","einstein"]],
      "collation",[
        "collationQuery","(aber t) einstein",
        "hits",375,
        "misspellingsAndCorrections",[
          "abert","aber t",
          "enstin","einstein"]],
      "collation",[
        "collationQuery","albert (ens tin)",
        "hits",335,
        "misspellingsAndCorrections",[
          "abert","albert",
          "enstin","ens tin"]],
      "collation",[
        "collationQuery","albert enshrin",
        "hits",137,
        "misspellingsAndCorrections",[
          "abert","albert",
          "enstin","enshrin"]],
      "collation",[
        "collationQuery","alert einstein",
        "hits",139,
        "misspellingsAndCorrections",[
          "abert","alert",
          "enstin","einstein"]]]}}

我正在尝试获取以下字段

"collation",[
        "collationQuery","albert einstein",
        "hits",154,
        "misspellingsAndCorrections",[
          "abert","albert",
          "enstin","einstein"]],

特别是"阿尔伯特·爱因斯坦"。我尝试了下面的代码,但出现错误。

$myArray = json_decode($response, true);
foreach ($myArray['collation'] as $doc) {
echo $doc[0];
 }

这里有很多问题。

  1. 排序规则不是 JSON 的根级字段。 responseHeaderresponsespellcheck是根级字段。其他所有内容都嵌套在它们下面。print_r($myArray) json_decode正在解析的内容。
  2. $myArray是一个对象,而不是一个数组。对象的访问方式与$myArray->response,而不是$myArray['response']
  3. 你所说的"田野"不是场。 json_encode认为该数据的结构为:

                [6] => collation
                [7] => Array
                    (
                        [0] => collationQuery
                        [1] => albert einstein
                        [2] => hits
                        [3] => 154
                        [4] => misspellingsAndCorrections
                        [5] => Array
                            (
                                [0] => abert
                                [1] => albert
                                [2] => enstin
                                [3] => einstein
                            )
                    )
    

如果这是输出 JSON 的代码,您将需要更好地构建它。如果这是别人的代码,他们是卑鄙的。