import json,re
text = '{"restName":"apple","restaurantTotal":150,"vouchers":null}'
obj = json.loads(text)
print(obj)
if 'banana' not in obj['restName'] and obj['restaurantTotal'] <= 200:
obj['vouchers'] = '[{"Discount":'+ str(obj['restaurantTotal']/2) +'}]'
result = json.dumps(obj ,separators=(',', ':')).replace("\\","")
print(result)
结果
{"restName":"apple","restaurantTotal":150,"vouchers":"[{"Discount":75.0}]"}
我想删除这部分的双引号只有(")[{"Discount":75.0}](")
有什么办法吗?
1条答案
按热度按时间dy2hfwbg1#
不要将值写成字符串。
json.dumps
可以处理这个问题。