douwen3083 2017-03-14 10:10
浏览 90
已采纳

在PHP中解析无效的JSON

I have this little problem. I have this linux machine outputting this kind of JSON:

xxx.xx.x.x | SUCCESS => {
    "architecture": "amd64", 
    "blockdevice_fd0_size": 0, 
    "blockdevice_sda_model": "Virtual disk", 
    "blockdevice_sda_size": 21474836480, 
    "blockdevice_sda_vendor": "VMware", 
    "blockdevice_sr0_model": "VMware IDE CDR10", 
... etc ... etc...
}

As you can see, its not valid... I would like to parse this file as a json. I know I somehow have to get rid of these xxx.xx.x.x | SUCCESS=> -lines.Regexp` maybe? There are many entries in file. I tried strstr with for loop, some regexp and so. If someone could give me a pointer :). Thanks in advance.

  • 写回答

3条回答 默认 最新

  • douzhuang2570 2017-03-14 10:15
    关注

    First, we will have to remove the IP address using the regex. Then You can use PHP and simply explode the string ` and get your JSON from the resulting array.

    $var = '192.168.1.1 | SUCCESS => {    "architecture": "amd64",      "lsbdistcodename": "wheezy",    "lsbdistdescription": "Debian GNU/Linux 7.9 (wheezy)",    "lsbdistid": "Debian",    "lsbdistrelease": "7.9",    "lsbmajdistrelease": "7",    "lsbminordistrelease": "9",     "memoryfree": "653.05 MB",    "memoryfree_mb": "653.05",    "memorysize": "1002.94 MB",    "memorysize_mb": "1002.94",     "operatingsystem": "Debian",    "operatingsystemmajrelease": "7",    "operatingsystemrelease": "7.9",    "os": {        "family": "Debian",        "lsb": {            "distcodename": "wheezy",            "distdescription": "Debian GNU/Linux 7.9 (wheezy)",            "distid": "Debian",            "distrelease": "7.9",            "majdistrelease": "7",            "minordistrelease": "9"        },        "name": "Debian",        "release": {            "full": "7.9",            "major": "7",            "minor": "9"        }    },    "osfamily": "Debian",    "partitions": {        "sda1": {            "filesystem": "LVM2_member",            "size": "41938944"        }    },     "physicalprocessorcount": 1,    "processor0": "Intel(R) Xeon(R) CPU E5-2620 v3 @ 2.40GHz",    "processorcount": 1,    "processors": {        "count": 1,        "models": [            "Intel(R) Xeon(R) CPU E5-2620 v3 @ 2.40GHz"        ],        "physicalcount": 1     },    "timezone": "EET",    "uniqueid": "007f0101",    "uptime": "360 days",    "uptime_days": 360,    "uptime_hours": 8644,    "uptime_seconds": 31119057,    "virtual": "vmware"}192.168.1.1 | SUCCESS => {    "architecture": "x86_64",       "is_virtual": true,    "kernel": "Linux",    "kernelmajversion": "3.10",    "kernelrelease": "3.10.0-514.2.2.el7.x86_64",    "kernelversion": "3.10.0",    "macaddress": "00:50:56:b1:c6:49",    "macaddress_eno16777984": "00:50:56:b1:c6:49",    "memoryfree": "1.58 GB",    "memoryfree_mb": "1617.09",    "memorysize": "1.80 GB",     "operatingsystemmajrelease": "7",    "operatingsystemrelease": "7.3.1611",    "os": {        "family": "RedHat",        "name": "CentOS",        "release": {            "full": "7.3.1611",            "major": "7",            "minor": "3"        }    },    "osfamily": "RedHat",    "partitions": {        "sda1": {            "filesystem": "xfs",            "mount": "/boot",            "size": "1024000",            "uuid": "e50a313f-98fa-4d7d-a40d-eb0c3b5f3ef5"        },        "sda2": {            "filesystem": "LVM2_member",            "size": "30423040"        }    },    "path": "/usr/local/bin:/usr/bin",    "physicalprocessorcount": 1,    "processor0": "Intel(R) Xeon(R) CPU E5-2620 v3 @ 2.40GHz",    "processorcount": 1,    "processors": {        "count": 1,        "models": [            "Intel(R) Xeon(R) CPU E5-2620 v3 @ 2.40GHz"        ],        "physicalcount": 1    },     "timezone": "EET",    "uniqueid": "44d40600",    "uptime": "87 days",    "uptime_days": 87,    "uptime_hours": 2094,    "uptime_seconds": 7539660,    "virtual": "vmware"';
    
    $pattern = '/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(?:\/\d{2})?/';
    $replacement = '';
    $result =  preg_replace($pattern, $replacement, $var); // Remove the IP address
    echo "$result".PHP_EOL;
    $arr = explode("| SUCCESS =>", $result); //Explode the string by the constant string
    
    print_r($arr);
    

    Resulting Array

    The first element will be empty.
    Array
    (
        [0] =>  
        [1] =>  {    "architecture": "amd64",      "lsbdistcodename": "wheezy",    "lsbdistdescription": "Debian GNU/Linux 7.9 (wheezy)",    "lsbdistid": "Debian",    "lsbdistrelease": "7.9",    "lsbmajdistrelease": "7",    "lsbminordistrelease": "9",     "memoryfree": "653.05 MB",    "memoryfree_mb": "653.05",    "memorysize": "1002.94 MB",    "memorysize_mb": "1002.94",     "operatingsystem": "Debian",    "operatingsystemmajrelease": "7",    "operatingsystemrelease": "7.9",    "os": {        "family": "Debian",        "lsb": {            "distcodename": "wheezy",            "distdescription": "Debian GNU/Linux 7.9 (wheezy)",            "distid": "Debian",            "distrelease": "7.9",            "majdistrelease": "7",            "minordistrelease": "9"        },        "name": "Debian",        "release": {            "full": "7.9",            "major": "7",            "minor": "9"        }    },    "osfamily": "Debian",    "partitions": {        "sda1": {            "filesystem": "LVM2_member",            "size": "41938944"        }    },     "physicalprocessorcount": 1,    "processor0": "Intel(R) Xeon(R) CPU E5-2620 v3 @ 2.40GHz",    "processorcount": 1,    "processors": {        "count": 1,        "models": [            "Intel(R) Xeon(R) CPU E5-2620 v3 @ 2.40GHz"        ],        "physicalcount": 1     },    "timezone": "EET",    "uniqueid": "007f0101",    "uptime": "360 days",    "uptime_days": 360,    "uptime_hours": 8644,    "uptime_seconds": 31119057,    "virtual": "vmware"} 
        [2] =>  {    "architecture": "x86_64",       "is_virtual": true,    "kernel": "Linux",    "kernelmajversion": "3.10",    "kernelrelease": "3.10.0-514.2.2.el7.x86_64",    "kernelversion": "3.10.0",    "macaddress": "00:50:56:b1:c6:49",    "macaddress_eno16777984": "00:50:56:b1:c6:49",    "memoryfree": "1.58 GB",    "memoryfree_mb": "1617.09",    "memorysize": "1.80 GB",     "operatingsystemmajrelease": "7",    "operatingsystemrelease": "7.3.1611",    "os": {        "family": "RedHat",        "name": "CentOS",        "release": {            "full": "7.3.1611",            "major": "7",            "minor": "3"        }    },    "osfamily": "RedHat",    "partitions": {        "sda1": {            "filesystem": "xfs",            "mount": "/boot",            "size": "1024000",            "uuid": "e50a313f-98fa-4d7d-a40d-eb0c3b5f3ef5"        },        "sda2": {            "filesystem": "LVM2_member",            "size": "30423040"        }    },    "path": "/usr/local/bin:/usr/bin",    "physicalprocessorcount": 1,    "processor0": "Intel(R) Xeon(R) CPU E5-2620 v3 @ 2.40GHz",    "processorcount": 1,    "processors": {        "count": 1,        "models": [            "Intel(R) Xeon(R) CPU E5-2620 v3 @ 2.40GHz"        ],        "physicalcount": 1    },     "timezone": "EET",    "uniqueid": "44d40600",    "uptime": "87 days",    "uptime_days": 87,    "uptime_hours": 2094,    "uptime_seconds": 7539660,    "virtual": "vmware"
    )
    

    Note : This regex is for IPV4 addresses that I got from IPV4 regex. You can change the regex for removing the IPV6 addresses too.

    Working IDEONE Link

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?