繁体   English   中英

如何使用不带引号的键从字符串中生成有效的JSON?

[英]how to make a valid JSON out of a string with non-quoted keys?

我有Followign 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,...等等。 我怎么能去回合之前,替换所有的话:为一个字符串? 谢谢

根据@ 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"}

暂无
暂无

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

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