0

I can't seem to wrap my head around this. I am getting json data into my php script in the following format:

![json structure][1]

I want to loop through "OnlinePlayers"... which, from reading other questions here, seems to be at

$json->{'ServerStatus'}->{'OnlinePlayers'};

assuming I decode it into an object and not an array, as

$json = json_decode($_POST["jsonData"]);

I have tried var_dump() to get the results out of that... I have tried adding "true" to the decode to get an array and then var_dump() on that - I can't get the result! I know it's there because if I just print out the raw $_POST["jsonData"], I see it there.

It's terribly formatted and I'm sorry for the eyesore, only including it here to show the data IS there. Can anyone point me in the right direction here? I just want each "Name" inside of "OnlinePlayers" (inside of "ServerStatus").

{
    "Plugins": [
        {
            "Name": "GroupManager v2.0 (Dev2.13.14) (Phoenix)"
        },
        {
            "Name": "WorldEdit v5.5.8"
        },
        {
            "Name": "BukkitCompat vR22A"
        },
        {
            "Name": "Websend v2.5.1"
        },
        {
            "Name": "Vault v1.2.27-b349"
        },
        {
            "Name": "dynmap v1.9.1-869"
        },
        {
            "Name": "Essentials vDev2.13.14"
        },
        {
            "Name": "Citizens v2.0.11-SNAPSHOT (build 1026)"
        },
        {
            "Name": "EssentialsProtect vDev2.13.14"
        },
        {
            "Name": "EssentialsSpawn vDev2.13.14"
        },
        {
            "Name": "Sentry v1.7.1"
        },
        {
            "Name": "EssentialsGeoIP vDev2.13.14"
        },
        {
            "Name": "EssentialsChat vDev2.13.14"
        },
        {
            "Name": "EssentialsAntiBuild vDev2.13.14"
        }
    ],
    "Invoker": {
        "XP": 0.4446353614330292,
        "IP": "/81.31.95.254:63218",
        "FoodLevel": 17,
        "CurrentItemIndex": 4,
        "Location": {
            "World": "Runic Paradise",
            "Yaw": 60.18408203125,
            "Y": 122.45612876403841,
            "X": 6645.424098440775,
            "Pitch": 28.879852294921875,
            "Z": -23.14999955097173
        },
        "XPLevel": 526,
        "Health": 19,
        "Inventory": [
            {
                "Durability": 0,
                "Amount": 64,
                "Type": 89,
                "Slot": 0
            },
            {
                "Durability": 0,
                "Amount": 2,
                "Type": 363,
                "Slot": 1
            },
            {
                "Data": 11,
                "Durability": 11,
                "Amount": 1,
                "Type": 35,
                "Slot": 2
            },
            {
                "Data": 14,
                "Durability": 14,
                "Amount": 1,
                "Type": 35,
                "Slot": 3
            },
            {
                "Durability": 0,
                "Amount": 4,
                "Type": 334,
                "Slot": 4
            },
            {
                "Data": 7,
                "Durability": 7,
                "Amount": 1,
                "Type": 35,
                "Slot": 5
            },
            {
                "Data": 44,
                "Durability": 16428,
                "Amount": 1,
                "Type": 373,
                "Slot": 6
            },
            {
                "Durability": 0,
                "Amount": 1,
                "Type": 44,
                "Slot": 7
            },
            {
                "Data": 7,
                "Durability": 7,
                "Amount": 1,
                "Type": 44,
                "Slot": 8
            },
            {
                "Durability": 0,
                "Amount": 2,
                "Type": 369,
                "Slot": 9
            }
        ],
        "Exhaustion": 2.9359312057495117,
        "Name": "runelynx",
        "CurrentItemID": 334,
        "GameMode": "CREATIVE",
        "IsOP": true
    },
    "ServerStatus": {
        "MaxMemory": 1413021696,
        "AvailableMemory": 121421064,
        "OnlinePlayers": [
            {
                "Name": "runelynx",
                "IP": "/81.31.95.254:63218"
            },
            {
                "Name": "BenBestvater",
                "IP": "/192.0.155.77:53885"
            }
        ]
    },
    "ServerSettings": {
        "NetherEnabled": true,
        "Name": "Runic Paradise",
        "Port": 25565,
        "Build": "git-Bukkit-1.6.4-R2.0-22-g40a262b-b2940jnks (MC: 1.7.2)",
        "MaxPlayers": 35,
        "FlyingEnabled": false,
        "OnlineMode": true,
        "DefaultGameMode": "SURVIVAL"
    }
}

////EDIT////

Note this PHP script is being called by my game server so I can't see any results displayed via HTML; instead Im writing to my DB to try and see the results... and after using all the examples below, the end result in the DB is just a blank cell. Such as the example below:

$json = json_decode($_POST["jsonData"]);
foreach ($json->ServerStatus->OnlinePlayers as $player){
    $pname .= $player->Name;
}

////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
$mysqli = new mysqli("asdf", "asdf", "asdf", "asdf");
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$mysqli->query("INSERT INTO log (Notes)
VALUES ('". $pname ."')");
runelynx
  • 403
  • 2
  • 4
  • 17
  • 1
    the json php extension is installed? json_decode is correct. – skrilled Dec 05 '13 at 20:00
  • 1
    once you decode json in php, it's a php data structure. You'd access like you would any OTHER php data structure. – Marc B Dec 05 '13 at 20:00
  • thanks. Now its completely unreadable above thanks to the edit someone just made :( – runelynx Dec 05 '13 at 20:05
  • @skrilled The JSON extension has been built into PHP since 5.2.0. – Brad Dec 05 '13 at 20:05
  • 1
    Assuming things is not how you solve problems. I answered a question a few weeks ago in which someone had to install the php5-json package to resolve their problem. Thanks though :D – skrilled Dec 05 '13 at 20:07
  • var_dump($json) returns nothing – runelynx Dec 05 '13 at 20:07
  • 1
    Json is now readable, use a validator like http://jsonlint.com/ to cleanup format. :) – Lohardt Dec 05 '13 at 20:14
  • I found a help article from my host that says json needs to be enabled... I just dont have access to do it... calling them up. http://support.godaddy.com/help/article/7853/working-with-json – runelynx Dec 05 '13 at 20:40

3 Answers3

1

PHP's magic quoting might be interfering with the post data, try this (and do check $json for null, it's the only way json_decode will tell you anything went wrong):

$json = json_decode(stripslashes($_POST['jsonData']));
if ($json === NULL) {
  error_log("json could not be decoded: " . json_last_error_msg());
}

More at the PHP runtime config.

user2926055
  • 1,963
  • 11
  • 10
1

this worked for me:

$json = json_decode($_POST["jsonData"]);
$onlinePlayers = $json->ServerStatus->OnlinePlayers;

foreach ($onlinePlayers as $player){
    print_r($player);
}
Lohardt
  • 1,057
  • 1
  • 12
  • 26
1

you have your OnlinePlayers as an attribute for ServerStatus so you can have it like this:

$json = json_decode($_POST["jsonData"]);
foreach ($json->ServerStatus->OnlinePlayers as $player){
    var_dump($player);
    echo '<br />';
}

check the fiddle

Update: another fiddle that echos the Name of the player.

mamdouh alramadan
  • 8,349
  • 6
  • 36
  • 53
  • Pls see the edit at the bottom of the question now? I can see the fiddle works but for some reason I'm getting nothing back. – runelynx Dec 05 '13 at 20:35
  • 1
    @runelynx - check the second fiddle, the Name being echoed correctly. you need to make sure that DB params are all ok. – mamdouh alramadan Dec 05 '13 at 20:44
  • my host confirmed JSON is off by default,I need to manually activate it... doing that and trying this all again.......... – runelynx Dec 05 '13 at 20:45
  • 1
    @runelynx - then you have your answer :) – mamdouh alramadan Dec 05 '13 at 20:45
  • still nada... I changed this php.ini file they have in my account but the behavior is still the same - using identical code to the fiddle, I get an empty $json... its like the decode doesnt do anything. Im thinking maybe the server needs to reload the ini or restart or something, calling them back ;) – runelynx Dec 05 '13 at 20:59
  • 1
    @runelynx - after ini changing you need to restart apache for sure. – mamdouh alramadan Dec 05 '13 at 20:59
  • my phpinfo says JSON is enabled, but still no results :( www.runic-paradise.com/phpinfo.php – runelynx Dec 05 '13 at 21:06
  • 1
    try `$json = substr($json, 3);` then decode it. check [this link](http://stackoverflow.com/questions/689185/json-decode-returns-null-after-webservice-call) for informaiton – mamdouh alramadan Dec 05 '13 at 21:09
  • still no dice :( dug further into that thread though, I see someone mentions magic quotes... in my phpinfo it says: magic_quotes_gpc=ON ... seeing if I can do something with that – runelynx Dec 05 '13 at 21:20
  • 1
    @runelynx = you can close this thread and open new one for json_decode failure – mamdouh alramadan Dec 05 '13 at 21:22
  • 1
    ohmygod!! IT WORKS!!!!!!!!!!!!!!!!! if(get_magic_quotes_gpc()){ $d = stripslashes($_POST['param']); }else{ $d = $_POST['param']; } Solved my problem!!!!!! – runelynx Dec 05 '13 at 21:23