繁体   English   中英

使用转义引号解析 JSON

[英]Parsing JSON with escape quote

我试图让我的 JSON 在我的项目中工作,但我卡在json.loads()上。 我不太明白为什么会发生这种情况,因为在我调用相同函数的其他设置上,它正在工作,但这个简单的例子不是。

test_json = """{
    "timestamp": "2021-12-20T14:15:31.835Z",
    "integrations": {
        "Optimizely": {
            "userId": "ad5903a8-f141-4b01-9890-631d1fcd5303"
        }
    },
    "context": {
        "traits": {
            "campaign_tracking_code": "",
            "dcg_profile_id": "OGEzOWU1ZmYtY2ZkYi00YTc3LWEzMmQtOTBjZDRhNGVkMTQy",
            "appsflyer_id": "",
            "mvpd": "Cablevision",
            "lastAnonymousProfileId": "",
            "lastKnownProfileId": "OGEzOWU1ZmYtY2ZkYi00YTc3LWEzMmQtOTBjZDRhNGVkMTQy",
            "us_privacy": "",
            "entitlements": "btn-btn2go,fbc-fox,FoxBusiness,foxdep,FoxNews,fs1,fs2",
            "isSignedIntoProfile": true,
            "network_entitlement_list": "btn-btn2go,fbc-fox,FoxBusiness,foxdep,FoxNews,fs1,fs2"
        },
        "device": {
            "id": "ad5903a8-f141-4b01-9890-631d1fcd5303"
        },
        "page": {
            "url": "https://www.fox.com/watch/45da27364675efe69099ee074d781178/",
            "path": "/watch/45da27364675efe69099ee074d781178/",
            "referrer": "",
            "search": "",
            "title": "Watch The Great North: Season 2, Episode 10, \"Dip the Halls Adventure\" Online - FOX"
        },
        "userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.93 Safari/537.36",
        "locale": "en-US",
        "library": {
            "name": "analytics.js",
            "version": "4.1.8"
        }
    },
    "properties": {
        "page_is_dark_mode": false,
        "page_login_state": "logged in",
        "primary_business_unit": "fng",
        "secondary_business_unit": "fox",
        "app_name": "foxnow",
        "app_platform": "web",
        "app_version": "3.46.1",
        "app_build": "289",
        "page_name": "delta:web:home:landing",
        "page_type": "home",
        "page_timePartingDay": "monday",
        "page_timePartingHour": "14:00",
        "authentication_status": true,
        "device_id": "ad5903a8-f141-4b01-9890-631d1fcd5303",
        "dma": "512",
        "event_type": "app",
        "profile_id": "OGEzOWU1ZmYtY2ZkYi00YTc3LWEzMmQtOTBjZDRhNGVkMTQy",
        "session_id": "b0b70c6f-c522-46d8-8689-ab46cf20cc82",
        "position": 4,
        "total_length": 30,
        "livestream": false,
        "asset_id": "508440479",
        "pod_id": "1",
        "title": "FOX/FOT_ALL_TMOBILE_PREPAID_DCM_VAST_315597834_Q421_30SV2",
        "season": "02",
        "episode": "10",
        "program": "The Great North",
        "content_asset_id": "1984523843962",
        "content_pod_ids": ["1", "2", "3", "4"],
        "video_asset_title": "Dip the Halls Adventure",
        "video_genre": "Drama",
        "video_is_livestream": false,
        "video_player_state": "play",
        "video_player_content_type": "ad",
        "video_asset_id": "45da27364675efe69099ee074d781178",
        "video_primary_business_unit": "fng",
        "video_secondary_business_unit": "fox",
        "video_fox_profile": true,
        "video_content_length": 1297,
        "video_content_type": "VOD",
        "video_content_length_format": "long-form",
        "video_station_id": "WBFF",
        "video_network": "fox",
        "video_seconds_viewed": 1,
        "video_freewheel_id": "1984523843962",
        "video_uid": "the-great-north_02_10",
        "video_series_name": "The Great North",
        "video_media_type": "video",
        "video_ad_supported": true,
        "video_ad_model": "linear",
        "video_rating": "TV-14",
        "video_asset_category": "fullEpisode",
        "video_season_number": "02",
        "video_episode_number": "10",
        "video_tms_id": "EP032285840022",
        "video_nielsen_clientid": "us-800251",
        "video_nielsen_subbrand": "c01",
        "video_affiliate_window": "none",
        "video_authorizing_network": "fox",
        "video_player_type": "standard player",
        "video_content_channel": "fox",
        "video_content_cdn": "edgecast",
        "video_content_platform": "vdms",
        "video_screen_layout": "no multiview",
        "video_airplay": false,
        "video_first_air_date": "2021-12-20T01:30:00.000Z",
        "video_first_digital_date": "2021-12-20T01:30:00.000Z",
        "video_originator": "fox",
        "video_cross_device_play": false,
        "video_content_subscription_type": "locked",
        "video_is_resume": false,
        "video_is_continuous": false,
        "video_is_restart": false,
        "video_is_autoplay": true,
        "video_is_fullscreen": false,
        "video_volume": 20,
        "video_playback_speed": "1",
        "video_max_bitrate": 0,
        "load_type": "dynamic",
        "rating": "TV-14",
        "video_current_bitrate": 1348471,
        "video_current_resolution": "594p",
        "ad_type": "preroll, midroll",
        "video_player": "jw",
        "sound": 20,
        "full_screen": false,
        "ad_enabled": true,
        "ad_asset_id": ["5", "0", "8", "4", "4", "0", "4", "7", "9", "19nb0l", "507246675", "53782914"],
        "publisher": "e2fd8af5-0279-4c82-ac9b-227c7bf7a354",
        "genre": "Drama",
        "channel": "fox",
        "full_episode": false,
        "airdate": "2021-12-20T01:30:00.000Z",
        "page_content_level_1": "delta:web:home",
        "page_content_level_2": "delta:web:home:landing",
        "page_content_level_3": "delta:web:home:landing",
        "page_content_level_4": "delta:web:home:landing",
        "favorites_personalities_length": 0,
        "favorites_content_list": "series:wwe friday night smackdown,series:the simpsons,series:game of talents,atlanta,archer,wicked tuna",
        "favorites_content_length": 6,
        "page_test_name": "[CPEOPT-358] Unlocked Homepage:Unlocked,SHOW: 404 Page Content - Round 2:Mosaic View,404 Page Click Tracking AA - 2:Original,Data Verification Test:Latest Episode Callout with CTA,CPEOPT-1050: Ungated Collection Title Bandit:Watch Free Episodes,CPEOPT-113: A/A Experiment #2 for Preroll Test:Treatment,[EXPLORE] Add Tubi Collection:Original",
        "page_content_personality": false,
        "page_banner_count": 1,
        "page_banner_list": "skinny banner:call me kat web promo banner:",
        "video_ad_break_type": "preroll",
        "video_ad_pod_type": "preroll",
        "video_ad_pod_position": 1,
        "ad_pod_id": 1,
        "type": "preroll",
        "pod_length": 2,
        "video_fw_vcid2": "e2fd8af5-0279-4c82-ac9b-227c7bf7a354",
        "video_ad_pod_id": 1,
        "video_fw_sitesection": "foxnow/webdesktop/vod/videopage",
        "video_ad_break_name": "preroll_1",
        "content": {
            "airdate": "2021-12-20T01:30:00.000Z",
            "asset_id": "1984523843962",
            "full_episode": false,
            "program": "The Great North",
            "title": "Dip the Halls Adventure",
            "load_type": "dynamic",
            "position": 0,
            "total_length": 30,
            "video_content_length": 1297,
            "video_nielsen_clientid": "us-800251",
            "video_nielsen_subbrand": "c01",
            "video_tms_id": "EP032285840022"
        },
        "pod_position": 1,
        "video_ad_name": "FOX/FOT_ALL_TMOBILE_PREPAID_DCM_VAST_315597834_Q421_30SV2",
        "video_ad_length": 30,
        "video_ad_id": "508440479",
        "video_ad_pod_name": "FOX/FOT_ALL_TMOBILE_PREPAID_DCM_VAST_315597834_Q421_30SV2",
        "video_ad_in_pod_position": 1,
        "video_ad_campaign_id": "7857_T-Mobile/PREPAID/UF/D/4Q21",
        "video_ad_creative_id": "162085221",
        "video_ad_advertiser": "T-MOBILE",
        "video_ad_creative_url": "https://adclick.g.doubleclick.net/pcs/click?xai=AKAOjsvX8p9A7WyAAsHHBTX5UPPdcSD1iieD3q5g4Vcb0bPq6nlrFkJYploQwbs3vHejGoHy8G2VTQXnHqcnMby_t2n53ATP6ubg4A-99LutCNUMaHKGcAaOvxOK1M_zPA&sig=Cg0ArKJSzCD2aOj0QSrXEAE&fbs_aeid=[gw_fbsaeid]&urlfix=1&adurl=https://www.metrobyt-mobile.com/shop/deals%3Fdclid%3D%25edclid!",
        "video_ad_is_skippable": false,
        "quartile": 1
    },
    "event": "Video Ad Playing",
    "messageId": "ajs-bc0f6f3382aac06685fd3b4c245a130f",
    "anonymousId": "ad5903a8-f141-4b01-9890-631d1fcd5303",
    "type": "track",
    "writeKey": "ZPcMFFdOZ2C5VbfBbO24WBHm6ywOeElz",
    "userId": "OGEzOWU1ZmYtY2ZkYi00YTc3LWEzMmQtOTBjZDRhNGVkMTQy",
    "sentAt": "2021-12-20T14:15:31.845Z",
    "_metadata": {
        "bundled": ["Chartbeat", "comScore", "DoubleClick Floodlight", "Google Tag Manager", "Nielsen DCR", "Nielsen DTVR", "Optimizely", "Segment.io"],
        "unbundled": [],
        "bundledIds": ["5f7de8baf732dc09661d4461", "609c3e921dac96336aba1d8f", "5acc7a0096b9d366b65c8fbf", "5c38ccc621e6e1000131ed3d", "5d558f1e6464af6391dc983a", "5d558ff8febd64575de04239", "F0jWvjADZB"]
    }
}"""

def home(request):
    json_data = json.loads(test_json)

    return render(request, 'parsers/home.html', {})

现在,如果我运行它,我会收到一个错误JSONDecode Error at /Expecting ',' delimiter: line 29 column 60 (char 957) 我相信这与正在发生的转义双引号有关。 我试图弄清楚在不更改 JSON 的情况下需要做什么才能使其工作。 我总是可以进行查找和替换,但我不想这样做,因为我的其他代码没有这样做。

谢谢

也许尝试用'替换转义引号( \"" ),然后再将其传递给json.loads()

所以,像这样:

def home(request):
    test_json = test_json.replace('"', "'")
    json_data = json.loads(test_json)
    return render(request, 'parsers/home.html', {})

这可能不是最好的解决方案,但它可以工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM