Python 将字符串转换为字典数组
我有以下形式的字符串:Python 将字符串转换为字典数组,python,json,python-2.7,Python,Json,Python 2.7,我有以下形式的字符串: "[{'status': 'Unshipped', 'city': 'New Delhi', 'buyer_name': 'xxx', 'name': 'xxx', 'countryCode': 'IN', 'payment_method': 'COD', 'y_id': 'xxx', 'phone': 'xxx', 'state': 'New Delhi', 'service': 'Expedited', 'amout': '425.00', 'address_1':
"[{'status': 'Unshipped', 'city': 'New Delhi', 'buyer_name': 'xxx', 'name': 'xxx', 'countryCode': 'IN', 'payment_method': 'COD', 'y_id': 'xxx', 'phone': 'xxx', 'state': 'New Delhi', 'service': 'Expedited', 'amout': '425.00', 'address_1': 'xxx', 'address_2': 'xxx', 'postalCode': '110018', 'shipped_by_y': 'false', 'channel': 'MFN', 'order_type': 'StandardOrder'}, {'status': 'Unshipped', 'city': 'thane', 'buyer_name': 'xxx', 'name': 'xxx', 'countryCode': 'IN', 'payment_method': 'COD', 'y_id': 'xxx', 'phone': 'xxx', 'state': 'Maharashtra', 'service': 'Expedited', 'amout': '350.00', 'address_1': 'xxx', 'address_2': 'xxx', 'postalCode': '400607', 'shipped_by_y': 'false', 'channel': 'MFN', 'order_type': 'StandardOrder'}]\n"
如何将此字符串转换为相应字典的数组
我尝试将字符串转换为JSON,然后加载JSON。但它仍然是一个字符串。这里没有JSON;这看起来像是您将带有字典的Python列表直接转换为字符串 用于将其转换回Python对象:
import ast
obj = ast.literal_eval(datastring)
literal\u eval将解析限制为仅对象文本元组、列表、字典、字符串、数字,避免了全面eval调用带来的任意代码执行风险
演示:
那么,您是如何将其转换为JSON的,以便在加载时它仍然是一个字符串?你能给我们举个真实的例子而不是虚构的吗?例如,键和值周围是否没有引号?@MartijnPieters:我在json.dumps后面加了json.loads。如果字符串是有效的json,请尝试只使用loads。@MartijnPieters:更新了question@nish:然后向我们显示该代码。你不在这里;JSON使用双引号。也许你丢弃了str的输出?
>>> import ast
>>> datastring = "[{'status': 'Unshipped', 'city': 'New Delhi', 'buyer_name': 'xxx', 'name': 'xxx', 'countryCode': 'IN', 'payment_method': 'COD', 'y_id': 'xxx', 'phone': 'xxx', 'state': 'New Delhi', 'service': 'Expedited', 'amout': '425.00', 'address_1': 'xxx', 'address_2': 'xxx', 'postalCode': '110018', 'shipped_by_y': 'false', 'channel': 'MFN', 'order_type': 'StandardOrder'}, {'status': 'Unshipped', 'city': 'thane', 'buyer_name': 'xxx', 'name': 'xxx', 'countryCode': 'IN', 'payment_method': 'COD', 'y_id': 'xxx', 'phone': 'xxx', 'state': 'Maharashtra', 'service': 'Expedited', 'amout': '350.00', 'address_1': 'xxx', 'address_2': 'xxx', 'postalCode': '400607', 'shipped_by_y': 'false', 'channel': 'MFN', 'order_type': 'StandardOrder'}]\n"
>>> ast.literal_eval(datastring)
[{'status': 'Unshipped', 'city': 'New Delhi', 'buyer_name': 'xxx', 'name': 'xxx', 'countryCode': 'IN', 'payment_method': 'COD', 'shipped_by_y': 'false', 'phone': 'xxx', 'state': 'New Delhi', 'service': 'Expedited', 'address_1': 'xxx', 'address_2': 'xxx', 'postalCode': '110018', 'order_type': 'StandardOrder', 'amout': '425.00', 'channel': 'MFN', 'y_id': 'xxx'}, {'status': 'Unshipped', 'city': 'thane', 'buyer_name': 'xxx', 'name': 'xxx', 'countryCode': 'IN', 'payment_method': 'COD', 'shipped_by_y': 'false', 'phone': 'xxx', 'state': 'Maharashtra', 'service': 'Expedited', 'address_1': 'xxx', 'address_2': 'xxx', 'postalCode': '400607', 'order_type': 'StandardOrder', 'amout': '350.00', 'channel': 'MFN', 'y_id': 'xxx'}]