无法访问json属性

问题描述 投票:0回答:1

我正在尝试访问此 json 的“city”属性,但不知何故它不起作用,这是 json 结构:

"{\"ForSaleShopperPlatformFullRenderQuery{\\\"zpid\\\":28657235,\\\"platform\\\":\\\"DESKTOP_WEB\\\",\\\"formType\\\":\\\"OPAQUE\\\",\\\"contactFormRenderParameter\\\":{\\\"zpid\\\":28657235,\\\"platform\\\":\\\"desktop\\\",\\\"isDoubleScroll\\\":true},\\\"skipCFRD\\\":false,\\\"ompPlatform\\\":\\\"web\\\"}\":{\"property\":{\"listingDataSource\":\"Phoenix\",\"zpid\":28657235,\"city\":\"Boerne\",\"state\":\"TX\",\"homeStatus\":\"FOR_SALE\",\"address\":{\"streetAddress\":\"111 stone creek\",\"city\":\"Boerne\",\"state\":\"TX\",\"zipcode\":\"78006\",\"neighborhood\":null,\"community\":null,\"subdivision\":null},\"isListingClaimedByCurrentSignedInUser\":false,\"isCurrentSignedInAgentResponsible\":false,\"bedrooms\":3,\"bathrooms\":2,\"price\":345000,\"yearBuilt\":1999,\"streetAddress\":\"111 stone creek\",\"zipcode\":\"78006\",\"isCurrentSignedInUserVerifiedOwner\":false,\"propertyUpdatePageLink\":null,\"moveHomeMapLocationLink\":null,\"propertyEventLogLink\":null,\"editPropertyHistorylink\":null,\"collections\":{\"modules\":[{\"name\":\"Similar homes\",\"placement\":\"NEIGHBORHOOD\",\"propertyDetails\":[{\"miniCardPhotos\":[{\"url\":\"https://photos.zillowstatic.com/fp/219307696092bb2d5e31698cba1c1e1f-p_c.jpg\"}],\"price\":315000,\"currency\":\"USD\",\"bedrooms\":3,\"bathrooms\":3,\"livingArea\":1729,\"livingAreaValue\":1729,\"livingAreaUnits\":\"Square Feet\",\"livingAreaUnitsShort\":\"sqft\",\"listingMetadata\":{\"comminglingCategoryIsRulesApplicable\":true},\"lotSize\":2874,\"lotAreaValue\":2874.96,\"lotAreaUnits\":\"Square Feet\",\"address\":{\"streetAddress\":\"130 Hampton Bend\",\"city\":\"Boerne\",

{
    "ForSaleShopperPlatformFullRenderQuery{\"zpid\":28657235,\"platform\":\"DESKTOP_WEB\",\"formType\":\"OPAQUE\",\"contactFormRenderParameter\":{\"zpid\":28657235,\"platform\":\"desktop\",\"isDoubleScroll\":true},\"skipCFRD\":false,\"ompPlatform\":\"web\"}": {
        "property": {
            "listingDataSource": "Phoenix",
            "zpid": 28657235,
            "city": "Boerne",
            "state": "TX",
            "homeStatus": "FOR_SALE",
            "address": {
                "streetAddress": "111 stone creek",
                "city": "Boerne",
                "state": "TX",
                "zipcode": "78006",
                "neighborhood": null,
                "community": null,
                "subdivision": null
            },

这是代码:

key = 'ForSaleShopperPlatformFullRenderQuery{"zpid":28657235,"platform":"DESKTOP_WEB","formType":"OPAQUE","contactFormRenderParameter":{"zpid":28657235,"platform":"desktop","isDoubleScroll":true},"skipCFRD":false,"ompPlatform":"web"}'
        city = data["props"]["pageProps"]["componentProps"]["gdpClientCache"][key]['property']['city']

我收到错误:

print(data["props"]["pageProps"]["componentProps"]["gdpClientCache"]['ForSaleShopperPlatformFullRenderQuery{\\"zpid\":28657235,\\"platform\\":\\"DESKTOP_WEB\\",\\"formType\\":\\"OPAQUE\\",\\"contactFormRenderParameter\\":{\\"zpid\\":28657235,\\"platform\\":\\"desktop\\",\\"isDoubleScroll\\":true},\\"skipCFRD\\":false,\\"ompPlatform\\":\\"web\\"}'])
          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: string indices must be integers, not 'str'

也尝试过

key = "ForSaleShopperPlatformFullRenderQuery{\"zpid\":28657235,\"platform\":\"DESKTOP_WEB\",\"formType\":\"OPAQUE\",\"contactFormRenderParameter\":{\"zpid\":28657235,\"platform\":\"desktop\",\"isDoubleScroll\":true},\"skipCFRD\":false,\"ompPlatform\":\"web\"}"

还是不行

python json python-3.x
1个回答
0
投票

首先,您的 json 示例不完整,因为我看不到这些键。

[“props”][“pageProps”][“componentProps”][“gdpClientCache”]

所以我尝试了

data[key]['property']['city']
,并且有效。 result

我觉得问题出在这个关键

gdpClientCache
。它必须返回一个字符串值,但不是您向我们展示的 json 示例。这就是为什么程序希望您使用整数作为索引。

仔细检查每个键的返回值,你应该能够解决问题。

© www.soinside.com 2019 - 2024. All rights reserved.