【问题标题】:decode json array with multiple objects in php [duplicate]在php中使用多个对象解码json数组[重复]
【发布时间】:2016-03-19 16:26:55
【问题描述】:

我无法解码包含多个对象的 JSON 数组。任何形式的帮助都会被充分利用。

    {  
   "article_details":[  
      {  
         "article_code ":"000000000300028156 ",
         "diff_amnt ":"1 "
      },
      {  
         "article_code ":"000000000300028174 ",
         "diff_amnt ":"1 "
      },
      {  
         "article_code ":"000000000300028126 ",
         "diff_amnt ":"1 "
      }
   ],
   "article":[  
      {  
         "article_code ":"000000000300028156 ",
         "diff_amnt ":"1 "
      },
      {  
         "article_code ":"000000000300028174 ",
         "diff_amnt ":"1 "
      },
      {  
         "article_code ":"000000000300028126 ",
         "diff_amnt ":"1 "
      }
   ]
}

【问题讨论】:

  • $arr = json_decode($yourArray, true);
  • 我已经浏览了上面的链接。但该代码无法自行运行!
  • 答案可以在这篇文章中找到:) 资源:stackoverflow.com/a/31355640/4275093
  • 实际上我有这些值,但我想知道我将如何分别选择文章值...!!

标签: php json


【解决方案1】:

使用json_decode() 解码 JSON 字符串,如下所示:

$json_array = json_decode($json, true);
                                  ^ When TRUE, returned objects will be converted into associative arrays.

所以你的代码应该是这样的:

// Here $json is your json string
$json_array = json_decode($json, true);

foreach($json_array as $key => $arrays){
    echo $key . "<br />";
    foreach($arrays as $array){
        foreach($array as $key => $value){
            echo $key . " => " . $value . "<br />";
        }
    }
    echo "<br />";
}

输出:

article_details
article_code => 000000000300028156
diff_amnt => 1
article_code => 000000000300028174
diff_amnt => 1
article_code => 000000000300028126
diff_amnt => 1

article
article_code => 000000000300028156
diff_amnt => 1
article_code => 000000000300028174
diff_amnt => 1
article_code => 000000000300028126
diff_amnt => 1 

【讨论】:

    【解决方案2】:

    试试这个:

    $json = '
    {
       "article_details":[  
          {  
             "article_code ":"000000000300028156 ",
             "diff_amnt ":"1 "
          },
          {  
             "article_code ":"000000000300028174 ",
             "diff_amnt ":"1 "
          },
          {  
             "article_code ":"000000000300028126 ",
             "diff_amnt ":"1 "
          }
       ],
       "article":[  
          {  
             "article_code ":"000000000300028156 ",
             "diff_amnt ":"1 "
          },
          {  
             "article_code ":"000000000300028174 ",
             "diff_amnt ":"1 "
          },
          {  
             "article_code ":"000000000300028126 ",
             "diff_amnt ":"1 "
          }
       ]
    }';
    
    $key = 'article_code '; // Since your key is 'article_code ' but not 'article_code', I have to do this
    $dataObject = json_decode($json);
    echo $dataObject->article_details[0]->$key; // This will out put: 000000000300028156 
    $dataArray = json_decode($json, true);
    echo $dataArray['article_details'][0]['article_code ']; // This will out put: 000000000300028156 
    

    【讨论】:

      【解决方案3】:

      使用 PHP json_decode 函数

      <?php
      $str = '{  
         "article_details":[  
            {  
               "article_code ":"000000000300028156 ",
               "diff_amnt ":"1 "
            },
            {  
               "article_code ":"000000000300028174 ",
               "diff_amnt ":"1 "
            },
            {  
               "article_code ":"000000000300028126 ",
               "diff_amnt ":"1 "
            }
         ],
         "article":[  
            {  
               "article_code ":"000000000300028156 ",
               "diff_amnt ":"1 "
            },
            {  
               "article_code ":"000000000300028174 ",
               "diff_amnt ":"1 "
            },
            {  
               "article_code ":"000000000300028126 ",
               "diff_amnt ":"1 "
            }
         ]
      }';
      
      var_dump(json_decode($str,true));
      

      输出:

      array(2) {
        ["article_details"]=>
        array(3) {
          [0]=>
          array(2) {
            ["article_code "]=>
            string(19) "000000000300028156 "
            ["diff_amnt "]=>
            string(2) "1 "
          }
          [1]=>
          array(2) {
            ["article_code "]=>
            string(19) "000000000300028174 "
            ["diff_amnt "]=>
            string(2) "1 "
          }
          [2]=>
          array(2) {
            ["article_code "]=>
            string(19) "000000000300028126 "
            ["diff_amnt "]=>
            string(2) "1 "
          }
        }
        ["article"]=>
        array(3) {
          [0]=>
          array(2) {
            ["article_code "]=>
            string(19) "000000000300028156 "
            ["diff_amnt "]=>
            string(2) "1 "
          }
          [1]=>
          array(2) {
            ["article_code "]=>
            string(19) "000000000300028174 "
            ["diff_amnt "]=>
            string(2) "1 "
          }
          [2]=>
          array(2) {
            ["article_code "]=>
            string(19) "000000000300028126 "
            ["diff_amnt "]=>
            string(2) "1 "
          }
        }
      }
      

      【讨论】:

        【解决方案4】:

        json_decode 的结果是一个具有两个属性的对象,article_detailsarticle
        它们中的每一个都是一个对象数组。

        但内部对象属性有一个尾随空格,"article_code ""diff_amnt "

        您可以按如下方式迭代它们

        $object = json_decode($str);
        
        foreach($object->article_details as $articleDetails){
        
            print $articleDetails->{"article_code "} . PHP_EOL;
        }
        
        
        foreach($object->article_details as $article){
            print $article->{"diff_amnt "} . PHP_EOL;
        }
        

        另请注意,这些值有一个尾随空格。
        要么修复它的来源,要么像这样过滤json 字符串中的尾随空格

        $jsonString = str_replace(' "','"',$jsonString);
        

        显式删除双引号前的任何尾随空格。
        然后以通常的方式访问对象属性

        foreach($object->article_details as $articleDetails){
            print $articleDetails->article_code . PHP_EOL;
        }
        
        
        foreach($object->article_details as $article){
            print $article->diff_amnt . PHP_EOL;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-02-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-03-18
          • 1970-01-01
          相关资源
          最近更新 更多