有没有办法纠正在PHP中无法解码的坏JSON

时间:2014-12-30 22:26:13

标签: php json

我从shell_exec服务中检索了一些JSON数据(从soundcloud中提取数据) - 但是当有多个条目时,PHP会在JSON上尝试json_decode时抛出错误4(json_last_error)

以下是JSON的一个示例:

{
    "display_id": "142993814",
    "upload_date": "20140404",
    "protocol": "http",
    "description": "~~~",
    "extractor": "soundcloud:set",
    "_filename": "/home/test/public/test.com/public/music_uploads/LYKOS - R E B I R T H.mp3",
    "uploader": "LYKOS",
    "duration": 240,
    "format_id": "http_mp3_128_url",
    "id": "142993814",
    "playlist_title": "LYKOS - R E B I R T H",
    "stitle": "R E B I R T H",
    "playlist": "LYKOS - R E B I R T H",
    "title": "R E B I R T H",
    "url": "https://ec-media.soundcloud.com/6phvZ45fCues.128.mp3?f10880d39085a94a0418a7ef69b03d522cd6dfee9399eeb9a522009566ffb6384d14cc1e49211bff90254e635b6f302484775d5b068cf60f94aba5cbe81c4fae5b4e5a43e2&AWSAccessKeyId=AKIAJNIGGLK7XA7YZSNQ&Expires=1419978413&Signature=hImHseUqea1dvmdpFBTv41s17WE%3D",
    "extractor_key": "SoundcloudSet",
    "vcodec": "none",
    "format": "http_mp3_128_url - audio only",
    "playlist_index": 1,
    "playlist_id": 63174483,
    "ext": "mp3",
    "webpage_url_basename": "lykos-r-e-b-i-r-t-h",
    "webpage_url": "http://soundcloud.com/lykosss/r-e-b-i-r-t-h",
    "formats": [
        {
            "url": "https://api.soundcloud.com/tracks/142993814/stream?client_id=b45b1aa10f1ac2941910a7f0d10f8e28",
            "format_id": "fallback",
            "ext": "mp3",
            "vcodec": "none",
            "format": "fallback - audio only"
        },
        {
            "protocol": "http",
            "format": "http_mp3_128_url - audio only",
            "url": "https://ec-media.soundcloud.com/6phvZ45fCues.128.mp3?f10880d39085a94a0418a7ef69b03d522cd6dfee9399eeb9a522009566ffb6384d14cc1e49211bff90254e635b6f302484775d5b068cf60f94aba5cbe81c4fae5b4e5a43e2&AWSAccessKeyId=AKIAJNIGGLK7XA7YZSNQ&Expires=1419978413&Signature=hImHseUqea1dvmdpFBTv41s17WE%3D",
            "vcodec": "none",
            "ext": "mp3",
            "format_id": "http_mp3_128_url"
        }
    ],
    "fulltitle": "R E B I R T H",
    "thumbnail": "https://i1.sndcdn.com/artworks-000099699341-ygkqvo-t500x500.jpg",
    "n_entries": 13
}{
    "display_id": "143857955",
    "upload_date": "20140409",
    "protocol": "http",
    "description": "~~~dreaming~~~",
    "extractor": "soundcloud:set",
    "_filename": "/home/test/public/test.com/public/music_uploads/LYKOS - S L E E P.mp3",
    "uploader": "LYKOS",
    "duration": 253,
    "format_id": "http_mp3_128_url",
    "id": "143857955",
    "playlist_title": "LYKOS - R E B I R T H",
    "stitle": "S L E E P",
    "playlist": "LYKOS - R E B I R T H",
    "title": "S L E E P",
    "url": "https://ec-media.soundcloud.com/8c5j72R7fUOY.128.mp3?f10880d39085a94a0418a7ef69b03d522cd6dfee9399eeb9a522009566ffb6384e10cc1e49211bff6d1fc6651c7f87f729f813831232cadee16ac0c95d88ce12e0292881ae&AWSAccessKeyId=AKIAJNIGGLK7XA7YZSNQ&Expires=1419978427&Signature=x60HoNiniDI1swnVXk%2F7fwXc6xo%3D",
    "extractor_key": "SoundcloudSet",
    "vcodec": "none",
    "format": "http_mp3_128_url - audio only",
    "playlist_index": 2,
    "playlist_id": 63174483,
    "ext": "mp3",
    "webpage_url_basename": "lykos-r-e-b-i-r-t-h",
    "webpage_url": "http://soundcloud.com/lykosss/s-l-e-e-p",
    "formats": [
        {
            "url": "https://api.soundcloud.com/tracks/143857955/stream?client_id=b45b1aa10f1ac2941910a7f0d10f8e28",
            "format_id": "fallback",
            "ext": "mp3",
            "vcodec": "none",
            "format": "fallback - audio only"
        },
        {
            "protocol": "http",
            "format": "http_mp3_128_url - audio only",
            "url": "https://ec-media.soundcloud.com/8c5j72R7fUOY.128.mp3?f10880d39085a94a0418a7ef69b03d522cd6dfee9399eeb9a522009566ffb6384e10cc1e49211bff6d1fc6651c7f87f729f813831232cadee16ac0c95d88ce12e0292881ae&AWSAccessKeyId=AKIAJNIGGLK7XA7YZSNQ&Expires=1419978427&Signature=x60HoNiniDI1swnVXk%2F7fwXc6xo%3D",
            "vcodec": "none",
            "ext": "mp3",
            "format_id": "http_mp3_128_url"
        }
    ],
    "fulltitle": "S L E E P",
    "thumbnail": "https://i1.sndcdn.com/artworks-000076040794-rjgkv4-t500x500.jpg",
    "n_entries": 13
}

有没有办法在php中修复JSON以便可解码?

1 个答案:

答案 0 :(得分:4)

您可以将其拆分为字符串,然后可以正确解析:

$twoJsons = explode('}{', $myInputString);

$obj1 = json_decode($twoJsons[0] . '}'); 
...

或只是将错误的}{替换为},{

$myJson = '[' . str_replace('}{', '},{', $myInputString) . ']';