dongshushen4392 2015-02-28 18:14
浏览 43
已采纳

PHP-从无效的json中删除密钥

I am new to php and dont know how to deal with invalid json. So I want to remove those keys which have invalid string values

Here tag_list and description have invalid string values.

$json='{
    "kind": "track",
    "id": 132453675,
    "created_at": "2014/02/01 02:01:11 +0000",
    "user_id": 895073,
    "duration": 323242,
    "commentable": true,
    "state": "finished",
    "original_content_size": 7745028,
    "last_modified": "2014/02/23 15:48:45 +0000",
    "sharing": "public",
    "tag_list": "hi " how are",
    "permalink": "leaman",
    "streamable": true,
    "embeddable_by": "all",
    "downloadable": false,
    "purchase_url": null,
    "label_id": null,
    "purchase_title": null,
    "genre": "Pop",
    "title": "Leaman",
    "description": "what"" is for",
    "label_name": null,
    "release": null,
    "track_type": null,
    "key_signature": null,
    "isrc": null,
    "video_url": null,
    "bpm": null,
    "release_year": null,
    "release_month": null,
    "release_day": null,
    "original_format": "mp3",
    "license": "all-rights-reserved",
    "uri": "https://api.soundcloud.com/tracks/132453675",
    "user": {
        "id": 895073,
        "kind": "user",
        "permalink": "cloudsareghosts",
        "username": "cloudsareghosts",
        "last_modified": "2014/02/13 22:16:09 +0000",
        "uri": "https://api.soundcloud.com/users/895073",
        "permalink_url": "http://soundcloud.com/cloudsareghosts",
        "avatar_url": "https://i1.sndcdn.com/avatars-000005407842-c7dt3c-large.jpg"
    },
    "permalink_url": "http://soundcloud.com/cloudsareghosts/leaman",
    "artwork_url": "https://i1.sndcdn.com/artworks-000069579796-mhc0bg-large.jpg",
    "waveform_url": "https://w1.sndcdn.com/r4xyDo8zqpfU_m.png",
    "stream_url": "https://api.soundcloud.com/tracks/132453675/stream",
    "playback_count": 253,
    "download_count": 0,
    "favoritings_count": 4,
    "comment_count": 0,
    "attachments_uri": "https://api.soundcloud.com/tracks/132453675/attachments",
    "policy": "ALLOW"
}';

So how can I remove these keys tag_list and description from this json in php.

Thanks

  • 写回答

1条回答 默认 最新

  • douyun4524 2015-02-28 18:21
    关注

    Seems like the double quotes messes up the JSON. You would have to replace these " with \"

    So: "description": "what"" is for", should be "description": "what\"\" is for",

    Depending on how you get the JSON, you could str_replace this with PHP instead of doing it manually. For example, if this JSON is created by a script of your own, you could edit that to make sure it outputs the backward slashes with it. (Looping of the object and relacing double quotes in the values by \".)

    EDIT: Example in PHP:

    $json = preg_replace_callback(
        '/^([^"]*"[^*]+": )"(.*?)"(.*)$/mU',
        function ($matches) {
            return $matches[1].'"'.str_replace('"', '\"', $matches[2]).'"'.$matches[3];
        },
        $json);
    

    This preg_replace_callback replaces the double quotes inside value declarations to \". See it here: https://eval.in/294426

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?