首页 > 解决方案 > 在 php 中解析无效 json_decode 数据的问题

问题描述

我需要解析数据并从可能不是有效 JSON 的 JSON 文件中提取 ID、用户名、电话

样本数据

 {""_"":""user"",""pFlags"":{""contact"":true},""flags"":2167,""id"":898989898,""access_hash"":""18303077651576387045"",""first_name"":""خشايار"",""last_name"":""هوروشي"",""phone"":""98203920399"",""photo"":{""_"":""userProfilePhoto"",""photo_id"":""1680040805486667797"",""photo_small"":{""_"":""fileLocation"",""dc_id"":4,""volume_id"":""455220331"",""local_id"":324641,""secret"":""3560097151242861840""},""photo_big"":{""_"":""fileLocation"",""dc_id"":4,""volume_id"":""455220331"",""local_id"":324643,""secret"":""4435530174476586386""}},""status"":{""_"":""userStatusRecently""}}"
{""_"":""user"",""pFlags"":{""contact"":true},""flags"":2103,""id"":9898989,""access_hash"":""10869201313646751836"",""first_name"":""هيرمند"",""last_name"":""فرزيني"",""phone"":""983049230490"",""photo"":{""_"":""userProfilePhoto"",""photo_id"":""2959023925263181761"",""photo_small"":{""_"":""fileLocation"",""dc_id"":4,""volume_id"":""455007904"",""local_id"":261742,""secret"":""351822579494173465""},""photo_big"":{""_"":""fileLocation"",""dc_id"":4,""volume_id"":""455007904"",""local_id"":261744,""secret"":""10983018442547226810""}}}"
{""_"":""user"",""pFlags"":{""contact"":true},""flags"":2167,""id"":9898989,""access_hash"":""10275071461967699194"",""first_name"":""داراب"",""last_name"":""فرزادي"",""phone"":""98901230910239"",""photo"":{""_"":""userProfilePhoto"",""photo_id"":""459395887189305518"",""photo_small"":{""_"":""fileLocation"",""dc_id"":4,""volume_id"":""455218467"",""local_id"":301592,""secret"":""9369292746617292051""},""photo_big"":{""_"":""fileLocation"",""dc_id"":4,""volume_id"":""455218467"",""local_id"":301594,""secret"":""13207755334439052246""}},""status"":{""_"":""userStatusOffline"",""was_online"":1564490108}}"
{""_"":""user"",""pFlags"":{""contact"":true},""flags"":2175,""id"":9898989,""access_hash"":""7793004574865111260"",""first_name"":""سوگند"",""last_name"":""شهرياري"",""username"":""Faflnasdasd"",""phone"":""98923821938"",""photo"":{""_"":""userProfilePhoto"",""photo_id"":""887478700670560930"",""photo_small"":{""_"":""fileLocation"",""dc_id"":4,""volume_id"":""455031395"",""local_id"":340581,""secret"":""15371281078852372263""},""photo_big"":{""_"":""fileLocation"",""dc_id"":4,""volume_id"":""455031395"",""local_id"":340583,""secret"":""2021643051882888603""}},""status"":{""_"":""userStatusOffline"",""was_online"":1567390764}}"

示例代码

parseData($input) {

    // Some code to extract specific data from invalid JSON

    return $data;
}

$string = file_get_contents("test.json");

$arr = parseData(json_decode(utf8_encode($string), true));

$s=0;

foreach($arr as $item=> $value){
    $s=count($value); // WIN
}

echo $s;

标签: phpjsonregexparsing

解决方案


如果您在诸如https://jsonlint.com/之类的 lint 工具中测试您的 json ,您会看到错误,并且第一个错误被双引号引用""以定义参数。

删除双引号后,分隔对象 {} 时出错。对于您需要逗号的每个对象,例如{},{},{}

最后一个错误是您有很多对象,要正常工作,您需要将对象放入数组中,例如[{},{},{}]

我为您的示例数据制作了一个可能的解决方案示例

[
{"_": "user","pFlags": {"contact": true},"flags": 2167,"id": 898989898,"access_hash": "18303077651576387045","first_name": "خشايار","last_name": "هوروشي","phone": "98203920399","photo": {"_": "userProfilePhoto","photo_id": "1680040805486667797","photo_small": {"_": "fileLocation","dc_id": 4,"volume_id": "455220331","local_id": 324641,"secret": "3560097151242861840"},"photo_big": {"_": "fileLocation","dc_id": 4,"volume_id": "455220331","local_id": 324643,"secret": "4435530174476586386"}},"status": {"_": "userStatusRecently"}}
,{"_": "user","pFlags": {"contact": true},"flags": 2103,"id": 9898989,"access_hash": "10869201313646751836","first_name": "هيرمند","last_name": "فرزيني","phone": "983049230490","photo": {"_": "userProfilePhoto","photo_id": "2959023925263181761","photo_small": {"_": "fileLocation","dc_id": 4,"volume_id": "455007904","local_id": 261742,"secret": "351822579494173465"},"photo_big": {"_": "fileLocation","dc_id": 4,"volume_id": "455007904","local_id": 261744,"secret": "10983018442547226810"}}}
,{"_": "user","pFlags": {"contact": true},"flags": 2167,"id": 9898989,"access_hash": "10275071461967699194","first_name": "داراب","last_name": "فرزادي","phone": "98901230910239","photo": {"_": "userProfilePhoto","photo_id": "459395887189305518","photo_small": {"_": "fileLocation","dc_id": 4,"volume_id": "455218467","local_id": 301592,"secret": "9369292746617292051"},"photo_big": {"_": "fileLocation","dc_id": 4,"volume_id": "455218467","local_id": 301594,"secret": "13207755334439052246"}},"status": {"_": "userStatusOffline","was_online": 1564490108}}
,{"_": "user","pFlags": {"contact": true},"flags": 2175,"id": 9898989,"access_hash": "7793004574865111260","first_name": "سوگند","last_name": "شهرياري","username": "Faflnasdasd","phone": "98923821938","photo": {"_": "userProfilePhoto","photo_id": "887478700670560930","photo_small": {"_": "fileLocation","dc_id": 4,"volume_id": "455031395","local_id": 340581,"secret": "15371281078852372263"},"photo_big": {"_": "fileLocation","dc_id": 4,"volume_id": "455031395","local_id": 340583,"secret": "2021643051882888603"}},"status": {"_": "userStatusOffline","was_online": 1567390764}}
]

推荐阅读