runelynx
runelynx

Reputation: 413

Parsing json data in php

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 ."')");

Upvotes: 0

Views: 147

Answers (3)

mamdouh alramadan
mamdouh alramadan

Reputation: 8528

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.

Upvotes: 1

Lohardt
Lohardt

Reputation: 1045

this worked for me:

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

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

Upvotes: 1

user2926055
user2926055

Reputation: 1991

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.

Upvotes: 1

Related Questions