0

I have this JSON-File here

{
"id" : "bf75b277-169b-49da-8ab1-b78b8dfg1b43-e25c7f28b3",
"ts" : "1372751172664",
"connected" : {
    "ssid" : "eduroam",
    "bssid" : "00:0f:f9:eb:08:81",
    "rssi" : "-62",
    "speed" : "53"
},
"configured" : [
    {
    "ssid" : "eduroam",
    "bssid" : "null",
    "keyMgmnt" : ["2", "3"],
    "grCiphers" : ["0","1","2","3"]
    },
    {
    "ssid" : "foobar",
    "bssid" : "null",
    "keyMgmnt" : ["0"],
    "grCiphers" : ["0","1","2","3"]
    }
],
"location" : {
    "prov" : "network",
    "lat" : "52.3793203",
    "lon" : "9.7231332",
    "acc" : "22.777"
    }
}

and I'm trying to get the key-value-pairs out into a file (and later into a mysql-database). I am having trouble to go along the nested structure. Maybe I do not understand it correctly?

$LOGPATH = "/var/www/test/";
$out = fopen($LOGPATH."testlog.log", "a");
$result = file_get_contents('php://input');
$data = json_decode($result, true);
$value = $data;

$test = array();

This line beneath causes me headaches, how can I say get the key-value-pairs of "connected"? Trying $test = $data['connected'] did not work, as the output simply is a "{" and nothing more...

$test = $data;

fwrite($out, "test \n");
fwrite($out, $test);
fwrite($out, "\n");

foreach ($test as $entry){
   fwrite($out, $entry);
   fwrite($out, "\n");
}

Any idea how to extract the key-value-pairs and/or help me understand the structure?

1
  • 1
    Use var_dump to understand the structure of your JSON array ($data). Commented Jul 7, 2013 at 18:37

3 Answers 3

2

This should get you on the right track

foreach($data['connected'] as $key => $value){
    echo 'Key: '.$key.' Value:'.$value.'<br>';
}
Sign up to request clarification or add additional context in comments.

Comments

2

json_decode() will dump JSON into regular array. You can do print_r() or var_dump() on it, to see the structure of the array. So to get your connected leaf you do:

$connected = $data['connected'];

You can then iterate over it:

foreach( $connected as $key=>$val ) {
    echo $key . ": " . $val;
}

2 Comments

When I try this, this happens: Invalid argument supplied for foreach()
use var_dump() to check what is content of vars passed. maybe your input data changed?
0

Got it, thanks to you guys!

// Get a request from i.e. a webpage (this is a webservice)
$jsonArray = file_get_contents('php://input');
// put the JSON-Data into an array
$jsonData = json_decode($jsonArray, true);
// You can simply choose the entry-points. As they do not change for me, they are hardcoded in here, else you had to use something like a for-loop to extract them
$jsonConnect = $jsonData['connected'];
$jsonLocation = $jsonData['location'];
$jsonConfigured = $jsonData['configured'];

// walk through the JSON-Data and extract the values, although the array has more than 2 levels, you can select your entry-point and walk from there on, which makes this quite easy
for($i = 0; $i < count($jsonConfigured); $i++){
        // keyMgmnt itself is an array and I want to concatenate all of its values and put it into a single field in my MySQL-DB, so implode() is used
        $keyMgmnt = implode(",", $jsonConfigured[$i]['keyMgmnt']);
        $grCiphers = implode(",", $jsonConfigured[$i]['grCiphers']);
        $ssid = $jsonConfigured[$i]['ssid'];
        $bssid = $jsonConfigured[$i]['bssid'];

        // from here on I simply put the variables into my MySQL-DB
        $sql_configured = "INSERT INTO configured (keyMgmnt, grCiphers, ssid, bssid) VALUES ('$keyMgmnt', '$grCiphers', '$ssid', '$bssid')";
        mysql_query($sql_configured) or die ("Failure in configured");
}

$sql_connected = "INSERT INTO connected (rssi, speed, ssid, bssid) VALUES ('$jsonConnect[rssi]', '$jsonConnect[speed]', '$jsonConnect[ssid]', '$jsonConnect[bssid]')";
$enterConnected = mysql_query($sql_connected) or die("Failure in connection!");
$sql_location = "INSERT INTO location (lat, prov, lon, acc) VALUES ('$jsonLocation[lat]', '$jsonLocation[prov]', '$jsonLocation[lon]', '$jsonLocation[acc]')";

$enterLocation = mysql_query($sql_location) or die("Failure in location!");

Thanks again!

Case closed.

PS: The structure of the JSON-Data. You get it by using "print_r()" or "var_dump()".

(
    [id] => bf75b277-169b-49da-8ab1-b78b80f51b43-e25c7f28b3
    [ts] => 1372751172664
    [connected] => Array
        (
        [ssid] => eduroam
        [bssid] => 00:0f:f7:eb:08:81
        [rssi] => -62
        [speed] => 54
    )

[configured] => Array
    (
        [0] => Array
            (
                [ssid] => eduroam
                [bssid] => null
                [keyMgmnt] => Array
                    (
                        [0] => 2
                        [1] => 3
                    )

                [grCiphers] => Array
                    (
                        [0] => 0
                        [1] => 1
                        [2] => 2
                        [3] => 3
                    )

            )

        [1] => Array
            (
                [ssid] => foobar
                [bssid] => null
                [keyMgmnt] => Array
                    (
                        [0] => 0
                    )

                [grCiphers] => Array
                    (
                        [0] => 0
                        [1] => 1
                        [2] => 2
                        [3] => 3
                    )

            )

    )

[location] => Array
    (
        [prov] => network
        [lat] => 52.3793203
        [lon] => 9.7231332
        [acc] => 22.777
    )

)

(
[id] => bf75b277-169b-49da-8ab1-b78b80f51b43-e25c7f28b3
[ts] => 1372751172664
[connected] => Array
    (
        [ssid] => eduroam
        [bssid] => 00:0f:f7:eb:08:81
        [rssi] => -62
        [speed] => 54
    )

[configured] => Array
    (
        [0] => Array
            (
                [ssid] => eduroam
                [bssid] => null
                [keyMgmnt] => Array
                    (
                        [0] => 2
                        [1] => 3
                    )

                [grCiphers] => Array
                    (
                        [0] => 0
                        [1] => 1
                        [2] => 2
                        [3] => 3
                    )

            )

        [1] => Array
            (
                [ssid] => foobar
                [bssid] => null
                [keyMgmnt] => Array
                    (
                        [0] => 0
                    )

                [grCiphers] => Array
                    (
                        [0] => 0
                        [1] => 1
                        [2] => 2
                        [3] => 3
                    )

            )

    )

[location] => Array
    (
        [prov] => network
        [lat] => 52.3793203
        [lon] => 9.7231332
        [acc] => 22.777
    )

)

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.