-3

i want to read out this json:

{ "name":"lexodexo", 
  "address":"lexodexo.de", 
  "port":"19132", 
  "month":"201602", 
  "voters": [ 
              { "nickname":"Henning", 
                 "votes":"6" 
               },
               { "nickname":"maxinator ", 
                 "votes":"5" 
               },
               { "nickname":"Blaubaer", 
                 "votes":"5" 
               }, 
               { "nickname":"Troll_Cyborg", 
                 "votes":"5" 
               }, 
               { "nickname":"OMG_ITS_INTOXx_", 
                 "votes":"2" } 
             ] 
}

And at the end it should look like this: Henning: 6 maxinator: 5 Blaubaer: 5

How can i do this?

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • 2
    Possible duplicate of [Handling data in a PHP JSON Object](http://stackoverflow.com/questions/263392/handling-data-in-a-php-json-object) – Leon Adler Feb 07 '16 at 19:55
  • It is unclear what you actually want as the result of the code that you want someone else to write for you should look like. Be more specific while writing your specification. **Oh hang on, SO is not a free coding or tutorial service** You have to show that you have made some effort to solve your own problem. Please read [What topics can I ask about](http://stackoverflow.com/help/on-topic) and [How to ask a good question](http://stackoverflow.com/help/how-to-ask) And [the perfect question](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) – RiggsFolly Feb 07 '16 at 19:59
  • Im stuck. I only get an "Array" Here is my code: – Lexodexo Feb 07 '16 at 20:24

2 Answers2

0

The JSON string is an object that contains a array of voters.

Dont use the ,true parameter in the json_decode() and then you get the original object as intended.

This would display the array.

<?php 
    $string = file_get_contents("minecraftpocket-servers.com/api/… ");
    $json = json_decode($string); 

    foreach ( $json->voters as $voter ) {
        echo $voter->nickname . ' = ' . $voter->votes . '<br>';
?>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
0

Please check the below code snippet for your requirement:

<?php
$jsonString =   '{ "name":"lexodexo", 
  "address":"lexodexo.de", 
  "port":"19132", 
  "month":"201602", 
  "voters": [ 
              { "nickname":"Henning", 
                 "votes":"6" 
               },
               { "nickname":"maxinator ", 
                 "votes":"5" 
               },
               { "nickname":"Blaubaer", 
                 "votes":"5" 
               }, 
               { "nickname":"Troll_Cyborg", 
                 "votes":"5" 
               }, 
               { "nickname":"OMG_ITS_INTOXx_", 
                 "votes":"2" } 
             ] 
}';

$jsonObj = json_decode($jsonString);
foreach ($jsonObj->voters as $result)
{
    $nickname = $result->nickname;
    $votes = $result->votes;

    echo $nickname."  ".$votes."<br>";
}
?>

The above code will output :

Henning 6
maxinator 5
Blaubaer 5
Troll_Cyborg 5
OMG_ITS_INTOXx_ 2

Hope this works for you!