使用PHP将JSON写入文件

时间:2013-07-07 18:34:50

标签: php mysql json

我这里有这个JSON文件

{
"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"
    }
}

我正在尝试将键值对输出到一个文件中(后来变成一个mysql数据库)。 我无法沿着嵌套结构前进。也许我不正确理解它?

$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();

这条线下面让我头疼,我怎么能说得到“连接”的键值对? 尝试$ test = $ data ['connected']不起作用,因为输出只是一个“{”而已......

$test = $data;

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

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

知道如何提取键值对和/或帮助我理解结构吗?

3 个答案:

答案 0 :(得分:2)

这可以让你走上正轨

foreach($data['connected'] as $key => $value){
    echo 'Key: '.$key.' Value:'.$value.'<br>';
}

答案 1 :(得分:2)

json_decode()会将JSON转储到常规数组中。您可以对其print_r()var_dump()进行查看,以查看数组的结构。所以要获得你的connected叶子:

$connected = $data['connected'];

然后你可以迭代它:

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

答案 2 :(得分:0)

知道了,谢谢你们!

// 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!");

再次感谢!

案件结案。

PS:JSON-Data的结构。你可以使用“print_r()”或“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
    )

)