首页 > 解决方案 > 如何从带有非引号键的字符串中生成有效的 JSON?

问题描述

我有以下python问题:

'{acorn: 15,acorn_type: 15,area_name: "London",beds_max: 1,beds_min: 1,branch_id: "21062",branch_name: "Realm Estates",brand_name: "Realm Estates",chain_free: false,company_id: "10832",country_code: "gb",county_area_name: "London",currency_code: "GBP",display_address: "Lancaster Mews, Wandsworth SW18",furnished_state: "unfurnished",group_id: "",has_epc: true,has_floorplan: false,incode: "1BA",is_retirement_home: false,is_shared_ownership: false,listing_condition: "pre-owned",listing_id: 45296714,listing_status: "to_rent",listings_category: "residential",location: "London",member_type: "agent",num_baths: 1,num_beds: 1,num_images: 5,num_recepts: 1,outcode: "SW18",post_town_name: "London",postal_area: "SW",price: 1300,price_actual: 1300,price_max: 1500,price_min: 1250,price_qualifier: "",property_highlight: "",property_type: "flat",region_name: "London",section: "to-rent",size_sq_feet: "",tenure: "",zindex: "686544"}'

我想要json.load()这个字符串,但显然所有的键都是错误的。它应该是'acorn: 15,...等等。我怎么能把 a 之前的所有单词都替换:成一个字符串?谢谢

标签: pythonregexstring

解决方案


根据@daniel-roseman 评论,最好使用 yaml 将字符串加载到 dict 中:

import yaml,json
string='{acorn: 15,acorn_type: 15,area_name: "London",beds_max: 1,beds_min: 1,branch_id: "21062",branch_name: "Realm Estates",brand_name: "Realm Estates",chain_free: false,company_id: "10832",country_code: "gb",county_area_name: "London",currency_code: "GBP",display_address: "Lancaster Mews, Wandsworth SW18",furnished_state: "unfurnished",group_id: "",has_epc: true,has_floorplan: false,incode: "1BA",is_retirement_home: false,is_shared_ownership: false,listing_condition: "pre-owned",listing_id: 45296714,listing_status: "to_rent",listings_category: "residential",location: "London",member_type: "agent",num_baths: 1,num_beds: 1,num_images: 5,num_recepts: 1,outcode: "SW18",post_town_name: "London",postal_area: "SW",price: 1300,price_actual: 1300,price_max: 1500,price_min: 1250,price_qualifier: "",property_highlight: "",property_type: "flat",region_name: "London",section: "to-rent",size_sq_feet: "",tenure: "",zindex: "686544"}'
dict_version = yaml.safe_load(string)
print dict_version

#if you need json version
json_version = json.dumps(dict_version)
print json_version

这给了你:

{'listing_status': 'to_rent', 'outcode': 'SW18', 'brand_name': 'Realm Estates', 'member_type': 'agent', 'chain_free': False, 'area_name': 'London', 'is_shared_ownership': False, 'country_code': 'gb', 'beds_max': 1, 'property_type': 'flat', 'incode': '1BA', 'furnished_state': 'unfurnished', 'branch_id': '21062', 'listing_id': 45296714, 'has_floorplan': False, 'beds_min': 1, 'section': 'to-rent', 'company_id': '10832', 'price_min': 1250, 'zindex': '686544', 'location': 'London', 'county_area_name': 'London', 'acorn_type': 15, 'is_retirement_home': False, 'price_max': 1500, 'listing_condition': 'pre-owned', 'display_address': 'Lancaster Mews, Wandsworth SW18', 'region_name': 'London', 'price': 1300, 'num_images': 5, 'listings_category': 'residential', 'branch_name': 'Realm Estates', 'post_town_name': 'London', 'acorn': 15, 'num_recepts': 1, 'size_sq_feet': '', 'price_qualifier': '', 'postal_area': 'SW', 'property_highlight': '', 'price_actual': 1300, 'num_baths': 1, 'num_beds': 1, 'has_epc': True, 'tenure': '', 'group_id': '', 'currency_code': 'GBP'}
{"listing_status": "to_rent", "outcode": "SW18", "brand_name": "Realm Estates", "member_type": "agent", "chain_free": false, "area_name": "London", "is_shared_ownership": false, "country_code": "gb", "beds_max": 1, "property_type": "flat", "incode": "1BA", "furnished_state": "unfurnished", "branch_id": "21062", "listing_id": 45296714, "has_floorplan": false, "beds_min": 1, "section": "to-rent", "company_id": "10832", "price_min": 1250, "zindex": "686544", "location": "London", "county_area_name": "London", "acorn_type": 15, "is_retirement_home": false, "price_max": 1500, "listing_condition": "pre-owned", "display_address": "Lancaster Mews, Wandsworth SW18", "region_name": "London", "price": 1300, "num_images": 5, "listings_category": "residential", "branch_name": "Realm Estates", "post_town_name": "London", "acorn": 15, "num_recepts": 1, "size_sq_feet": "", "price_qualifier": "", "postal_area": "SW", "property_highlight": "", "price_actual": 1300, "num_baths": 1, "num_beds": 1, "has_epc": true, "tenure": "", "group_id": "", "currency_code": "GBP"}

推荐阅读