1

I am unable to decode JSON array with multiple objects. any kind of help will be use full.

    {  
   "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 "
      }
   ]
}
Thamilhan
  • 13,040
  • 5
  • 37
  • 59
Mohit Gupta
  • 73
  • 1
  • 1
  • 7

4 Answers4

3

Give this a try:

$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 
kyo
  • 645
  • 4
  • 7
2

Use json_decode() to decode a JSON string, like this:

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

So your code should be like this:

// 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 />";
}

Output:

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 
Rajdeep Paul
  • 16,887
  • 3
  • 18
  • 37
0

Use PHP json_decode function

<?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));

Output:

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 "
    }
  }
}
Thamilhan
  • 13,040
  • 5
  • 37
  • 59
0

The result of json_decode is an object with two properties, article_details and article
each of them is an array of objects.

But the inner object properties have a trailing space, "article_code " and "diff_amnt "

You can iterate over them as follows

$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;
}

Also note that the values have a trailing space.
Either fix where this is coming from or filter the trailing spaces in the json string like this

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

Explicitly removing any trailing spaces before a double quote.
Then accessing the object properties in usual way

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


foreach($object->article_details as $article){
    print $article->diff_amnt . PHP_EOL;
}
Alex Andrei
  • 7,315
  • 3
  • 28
  • 42