我正在尝试访问这个 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\"}"
仍然不起作用
首先,您的 json 示例不完整,因为我看不到这些键。
所以我尝试了一下![结果](https://isstatic.askoverflow.dev/KrYg6LGy.png)
data[key]['property']['city']
,并且它有效。我认为问题出在这个键上
gdpClientCache
。它一定返回了一个字符串值,而不是您向我们展示的 json 示例。这就是为什么程序希望您使用整数作为索引。仔细检查每个键的返回值,你就应该能够解决问题。