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 "
      }
   ]
}
4
  • 1
    $arr = json_decode($yourArray, true); Commented Dec 14, 2015 at 8:53
  • i had gone through above link. but that code is not working it self!! Commented Dec 14, 2015 at 9:01
  • Answer can find in this article :) Resource: stackoverflow.com/a/31355640/4275093 Commented Dec 14, 2015 at 9:05
  • actually i have these values but i want to know how would i pick articles value separately...!! Commented Dec 14, 2015 at 9:16

4 Answers 4

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 
Sign up to request clarification or add additional context in comments.

Comments

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 

Comments

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

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.