在PHP中解析无效的JSON

在PHP中解析无效的JSON,php,json,Php,Json,我有个小问题。我有一台linux机器输出这种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",

我有个小问题。我有一台linux机器输出这种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...
}

正如你所看到的,它是无效的。。。我想将此文件解析为json。我知道我必须摆脱这些xxx.xx.x.x | SUCCESS=>-line.Regexp`也许?文件中有许多条目。我用for循环、一些regexp等尝试了strstr。如果有人能给我一个指针:。提前谢谢。

编辑:阿尤什找到了一种更漂亮的方式

是否可以跳转到第一个“{”,然后删除之前的内容

$findme   = '{';
$pos = strpos($json, $findme);
然后

$json= substr($json, $pos, 0);

首先,我们必须使用正则表达式删除IP地址,然后您可以使用PHP简单地分解字符串`并从结果数组中获取JSON

$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);
结果数组

注意:此正则表达式用于我从中获取的IPV4地址。您也可以更改正则表达式以删除IPV6地址


您也可以使用正则表达式:

// create the input json
$my_json = '212.68.0.4 | 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"}';

// search for all chars before "{" and replace it with the bracket ({) using \\1
$output_json = preg_replace("/.*({)/", "\\1", $my_json);

// show the output result
echo $output_json;

substr函数中的最后一个参数应该被省略,以便让整个jsonI都这样做。但是我想我需要stru替换。我需要去掉xxx.xx.x.x | SUCCESS=>-行。正常运行时间:8644小时,正常运行时间:31119057秒,虚拟:vmware}xxx.xx.x.x | SUCCESS=>{是的,我试过爆炸。结果是这样的:Array[0]=>xxx.x.x |成功[1]=>{体系结构:amd64,块设备(fd0)大小:0,块设备(sda)型号:虚拟磁盘,块设备(sda)大小:21474836480等..xxx.xx.x.x |成功[2]=>{体系结构:x86_64,blockdevice_fd0_大小:4096,blockdevice_sda_型号:虚拟磁盘,但我无法将其输出到json。我需要它,这样我就可以将其用作键:value:O。@user7190476你能给我完整的输入吗?这样,我就可以看到如何处理它。@user7190476使用json格式化程序检查你的输出JSON。好了。就是这样的信息。我拿出了一些。在那个粘贴中,有2个entries@user7190476此数据中有多个JSON。正在接近,但JSON_encode仍返回null,jsonlint不接受:/如果您在示例中使用JSON,则最后一部分是错误的,您可以使用如下内容:212.68.0.4 | SUCCESS=>{体系结构:amd64,块设备(fd0)大小:0,块设备(sda)型号:虚拟磁盘,块设备(sda)大小:21474836480,块设备(sda)供应商:VMware,块设备(sr0)型号:VMware IDE CDR10}
// create the input json
$my_json = '212.68.0.4 | 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"}';

// search for all chars before "{" and replace it with the bracket ({) using \\1
$output_json = preg_replace("/.*({)/", "\\1", $my_json);

// show the output result
echo $output_json;