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

Get all key value pairs from complex JSON payload using PHP

我有一个从服务器收到的 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 的知识解决它:)

Notice: Undefined index: payload_field in E:\xampp7\htdocs\test\test.php on line 49

Notice: Undefined index: gtw_id in E:\xampp7\htdocs\test\test.php on line 53

Notice: Undefined index: gateways in E:\xampp7\htdocs\test\test.php on line 54

Notice: Undefined index: gateways in E:\xampp7\htdocs\test\test.php on line 55

Notice: Undefined index: gateways in E:\xampp7\htdocs\test\test.php on line 56

Notice: Undefined index: gateways in E:\xampp7\htdocs\test\test.php on line 57

Notice: Undefined index: gateways in E:\xampp7\htdocs\test\test.php on line 58

Notice: Undefined variable: test1 in E:\xampp7\htdocs\test\test.php on line 78

所以我需要帮助。提前致谢。

有几个错误:

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

等等。

请再次检查您的json。

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

How to Flatten a Multidimensional Array?

从那时起,它应该是相当困难的了。

希望对您有所帮助。