如何解码php中的json

时间:2013-11-17 02:29:31

标签: php json

这是我的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 个答案:

答案 0 :(得分:2)

这里有很多问题。

  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
                            )
    
                    )
    
  4. 如果这是您输出JSON的代码,那么您将需要更好地构建它。如果这是别人的代码,那就是他们的意思。