使用PHP从复杂的JSON有效负载中获取所有键值对

时间:2019-07-16 21:01:18

标签: php json payload

我有一个从服务器接收到的JSON文件,无法随时更改。 我正在尝试使用PHP淘汰所有key:value对,但是我的成功是部分的。

有人因为我还没有找到答案来解决这种麻烦吗?

如您所见,我做了一些编码

<?php

$content = ('{
    "app_id": "test1",
    "dev_id": "temperature",
    "hardware_serial": "0056748F03A5C6CB1",
    "port": 1,
    "counter": 2217,
    "payload_raw": "AYgAAAAAAAAAAAA=",
    "payload_fields": {
        "height": 0,
        "pressure": 0,
        "temperature": 3.92
    },
    "metadata": {
        "time": "2019-07-16T20:39:37.761000811Z",
        "frequency": 867.9,
        "modulation": "LORA",
        "data_rate": "SF7BW125",
        "coding_rate": "4/5",
        "gateways": [
            {
                "gtw_id": "eui-b346ebfffec332dc",
                "timestamp": 2514820708,
                "time": "2019-07-16T20:39:37.735765Z",
                "channel": 7,
                "rssi": -65,
                "snr": 9.5,
                "rf_chain": 0,
                "latitude": 44.754036,
                "longitude": 19.690582,
                "location_source": "registry"
            }
        ]
    },
    "downlink_url": "https://integrations.net"}');


$mypayload = json_decode($content, true);


//decode and save JSON data

$AppID               = $mypayload['app_id'];
$DevID               = $mypayload['dev_id'];
$HardwareSerial      = $mypayload['hardware_serial'];
$Counter             = $mypayload['counter'];
$PayloadRaw          = $mypayload['payload_raw'];
$PayloadFields       = $mypayload['payload_field'];
$Time                = $mypayload['metadata']['time'];
$DataRate            = $mypayload['metadata']['data_rate'];
$CodingRate          = $mypayload['metadata']['coding_rate'];
$GtwID               = $mypayload['gtw_id'];
$GwTime              = $mypayload['gateways']['time'];
$GwRssi              = $mypayload['gateways']['rssi'];
$GwSnr               = $mypayload['gateways']['snr'];
$GwLatitude          = $mypayload['gateways']['latitude'];
$GwLongitude         = $mypayload['gateways']['longitude'];


$myfile     = fopen("C:/Users/Public/Documents/test2.txt","a+") or die("Unable to open file!");

fwrite($myfile, "App ID = " . $AppID . "\n" );
fwrite($myfile, "DevID = " . $DevID . "\n" );
fwrite($myfile, "HardwareSerial = " . $HardwareSerial . "\n" );
fwrite($myfile, "Counter = " . $Counter . "\n" );
fwrite($myfile, "PayloadRaw = " . $PayloadRaw . "\n" );
fwrite($myfile, "PayloadFields = " . $PayloadFields . "\n" );
fwrite($myfile, "Time    = " . $Time . "\n");
fwrite($myfile, "DataRate = " . $DataRate . "\n" );
fwrite($myfile, "CodingRate = " . $CodingRate . "\n" );
fwrite($myfile, "GtwID = " . $GtwID . "\n");
fwrite($myfile, "GwTime = " . $GwTime . "\n");
fwrite($myfile, "GwRssi = " . $GwRssi . "\n");
fwrite($myfile, "GwSnr = " . $GwSnr . "\n");
fwrite($myfile, "GwLatitude = " . $GwLatitude . "\n" );
fwrite($myfile, "GwLongitude = " . $GwLongitude . "\n" );
fwrite($myfile, "test1 = " . $test1 . "\n" );

fclose($myfile);




?>

我知道为什么会收到此错误,但是显然我对PHP的了解无法解决它:)

  

注意:未定义的索引:payload_field   第49行的E:\ xampp7 \ htdocs \ test \ test.php

     

注意:未定义的索引:gtw_id,位于E:\ xampp7 \ htdocs \ test \ test.php中   第53行

     

注意:未定义的索引:E:\ xampp7 \ htdocs \ test \ test.php中的网关   第54行

     

注意:未定义的索引:E:\ xampp7 \ htdocs \ test \ test.php中的网关   第55行

     

注意:未定义的索引:E:\ xampp7 \ htdocs \ test \ test.php中的网关   第56行

     

注意:未定义的索引:E:\ xampp7 \ htdocs \ test \ test.php中的网关   第57行

     

注意:未定义的索引:E:\ xampp7 \ htdocs \ test \ test.php中的网关   第58行

     

注意:未定义的变量:test1在E:\ xampp7 \ htdocs \ test \ test.php中   第78行

所以我需要帮助。预先感谢。

2 个答案:

答案 0 :(得分:1)

有几个错误:

  1. 索引必须为payload_fields,而不是payload_field
  2. 它必须是$mypayload["metadata"]["gateways"]["gtw_id"]而不是$mypayload["gtw_id"]
  3. 它必须是$mypayload["metadata"]["gateways"]而不是$mypayload["gateways"]

以此类推。

请再次检查您的json。

答案 1 :(得分:0)

将json转换为仅数组json_decode($ json_data,TRUE);比使用http://php.net/array_walk_recursive来将数组平面化为一维数组。

How to Flatten a Multidimensional Array?

从那时起,它应该向前挺了。

希望这会有所帮助。