0

I am trying to decode below JSON file contents with php.

"notification": {/* code related to notification */ },
"HOME_SCREEN": 
    {
        "Phone": 
        {
            "position": 1 ,                         
            "list": 
            [
                {
                    "position": 1,
                    "productID": "105"

                }
            ]
        },
    },
"notify": { /* code related to notify */},  

I followed links here & here & tried as below. but its giving blank page....

$string = file_get_contents("test.json");
$json_a = json_decode($string, true);

foreach ($json_a as $Phone => $person_a)
 {
   echo isset($person_a['position']);
 }

Also tried as :

$json_string = file_get_contents("test.json");
$decoded = json_decode($json_string);
$comments = $decoded->data[0]->comments->data;
foreach($comments as $comment){
   $position = $comment->position;   
   echo $comment['position'];
}   

and below:

$url = 'http://url.com/test.json';
$content = file_get_contents($url);
$json = json_decode($content, true);

foreach($json as $i){
    echo $i['position'];
}

Edit

checked Json file online, its valid json , same json file in better view : https://pastebin.com/mUEpqfaM

  • Have you just tried using `print_r` in the decoded json? Like that, you can check the parsed structure and increment the values from there. – FirstOne Dec 19 '17 at 13:25
  • What does your json look like exactly? What you have posted is not valid json. – jeroen Dec 19 '17 at 13:25
  • For validating json, you can use something like https://jsonformatter.curiousconcept.com/ – Chris Dec 19 '17 at 13:29
  • @FirstOne when i tried i got result as [here](http://sbdev1.kidsdial.com:81/home.php) –  Dec 19 '17 at 13:43
  • @Kaddath when i tried `json_decode` , i got result as [link2](http://sbdev1.kidsdial.com:81/home2.php) –  Dec 19 '17 at 13:48
  • @jeroen its valid json file , tested online = > [link3](http://sbdev1.kidsdial.com:81/test.json) –  Dec 19 '17 at 13:50
  • @Chris checked it, its valid json [link3](http://sbdev1.kidsdial.com:81/test.json) –  Dec 19 '17 at 13:50

1 Answers1

0

There is nothing more you can/should do with decoding of JSON file.

As written in php documentation for json_decode you are using $assoc = true so you're converting whole JSON file into associative array. It's up to you if you want to decode the JSON file into an OBJECT or ASSOCIATIVE ARRAY. Till this point, you have it correct.

For your understanding, the main difference is:

  • ARRAY, you access values with $json['notification']
  • OBJECT, you access values with $json->notification

I prefer ARRAY, as it's easier for me to navigate in and use foreach loops. As written in comments, you should check the structure of your decoded file, so you can understand how you can access values you're interested in.

Minimal code to do that is

$string = file_get_contents("test.json");
$json = json_decode($string, true);

echo "<pre>";
  print_r($json);
echo "</pre>";

Let's say you want to access the position in your $json array for HOME_SCREEN / Phone > list, for that, you need to use this foreach loop:

foreach ($json['HOME_SCREEN']['Phone']['list'] as $item) {
  echo $item['position'];
}
moped
  • 2,197
  • 2
  • 24
  • 30
  • thanks, in browser i need to give `edit option` for `position` value and once i click on `save button` position value should change & save file in json format, is this possible ? –  Dec 20 '17 at 07:42
  • please tell why this is not working : `foreach ($json['HOME_SCREEN']['Phone'] as $item) { echo $item['position'];` }? –  Dec 20 '17 at 07:55
  • That is because you don't have such a structure. When looping through `list`, you are accessing `$json['HOME_SCREEN']['Phone']['list'][0]['position']` (see that zero there?). In `$json['HOME_SCREEN']['Phone']` the position is directly in the `$item` variable as you're directly on that value, not on an array containing values like in `list` – moped Dec 20 '17 at 08:28
  • then is there any way to display values of `['HOME_SCREEN']['Phone']` ? i need these values : `"HOME_SCREEN":{ "Phone":{ "position":1, "isImage":"false" },` , full file : https://pastebin.com/cSn6eZeG –  Dec 20 '17 at 09:14
  • As I wrote, for that, simply `echo $item;` will display it. You can always check what is inside the variable by using `var_dump($item);` and that way you will see if you have a desired value there (value of position) or you have an array that has key->value (item['position']) – moped Dec 20 '17 at 09:16
  • thanks, can you help me how to get values of `price :R` & `price :D` here : `"HOME_SCREEN":{ "Phone":{ "price":{ "R":549, "D":15 },` –  Dec 20 '17 at 09:30
  • without loops, it's `$json['HOME_SCREEN']['Phone']['price']['R']` and `$json['HOME_SCREEN']['Phone']['price']['D']` .. in loop, that would need some more code, checking if it's string or array, what key is there etc. – moped Dec 20 '17 at 09:38
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/161606/discussion-between-vickey-colors-and-moped). –  Dec 20 '17 at 09:41