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 ."')");
8
  • 1
    the json php extension is installed? json_decode is correct. Commented Dec 5, 2013 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. Commented Dec 5, 2013 at 20:00
  • thanks. Now its completely unreadable above thanks to the edit someone just made :( Commented Dec 5, 2013 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 Commented Dec 5, 2013 at 20:07
  • 1
    Json is now readable, use a validator like jsonlint.com to cleanup format. :) Commented Dec 5, 2013 at 20:14

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

11 Comments

@runelynx - check the second fiddle, the Name being echoed correctly. you need to make sure that DB params are all ok.
@runelynx - then you have your answer :)
@runelynx - after ini changing you need to restart apache for sure.
try $json = substr($json, 3); then decode it. check this link for informaiton
@runelynx = you can close this thread and open new one for json_decode failure
|
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.

Comments

1

this worked for me:

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

foreach ($onlinePlayers as $player){
    print_r($player);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.