python str与json类型转换

本文介绍了如何在Python中进行str与json之间的转换。包括从str到json的转换,从json到str的转换,以及解决在str转json过程中遇到的`JSONDecodeError: Expecting property name enclosed in double quotes`问题。解决方案是将str中的单引号替换为双引号。

1.str 转json

import json
text = '{"sentences":[{"silence_duration":7,"end_time":19585,"speech_rate":71,"begin_time":18740,\
        "text":"喂","channel_id":1,"emotion_value":6.0},{"silence_duration":0,"end_time":26660,"speech_rate":334,\
        "begin_time":19660,"text":"喂您好,非常感谢您的接听啊,我这边是来自阳光电话车险的,呃,三10给您发的那个","channel_id":1,\
        "emotion_value":7.0}]}'
text_json = json.loads(text)
print("text type:",type(text))
print("text_json type:",type(text_json))

输出

text type: <class 'str'>
{'sentences': [{'silence_duration': 7, 'end_time': 19585, 'speech_rate': 71, 'begin_time': 18740, 'text': '喂', 'channel_id': 1, 'emotion_value': 6.0}, {'silence_duration': 0, 'end_time': 26660, 'speech_rate': 334, 'begin_time': 19660, 'text': '喂您好,非常感谢您的接听啊,我这边是来自阳光电话车险的,呃,三10给您发的那个', 'channel_id': 1, 'emotion_value': 7.0}]}
text_json type: <class 'dict'>

2.json 转str

import json
text = {"sentences":[{"silence_duration":7,"end_time":19585,"speech_rate":71,"begin_time":18740,\
        "text":"喂","channel_id":1,"emotion_value":6.0},{"silence_duration":0,"end_time":26660,"speech_rate":334,\
        "begin_time":19660,"text":"喂您好,非常感谢您的接听啊,我这边是来自阳光电话车险的,呃,三10给您发的那个","channel_id":1,\
        "emotion_value":7.0}]}
text_str = json.dumps(text)
print("text type:",type(text))
print(text_str)
print("text_str type:",type(text_str))

输出

text type: <class 'dict'>
{"sentences": [{"silence_duration": 7, "end_time": 19585, "speech_rate": 71, "begin_time": 18740, "text": "\u5582", "channel_id": 1, "emotion_value": 6.0}, {"silence_duration": 0, "end_time": 26660, "speech_rate": 334, "begin_time": 19660, "text": "\u5582\u60a8\u597d\uff0c\u975e\u5e38\u611f\u8c22\u60a8\u7684\u63a5\u542c\u554a\uff0c\u6211\u8fd9\u8fb9\u662f\u6765\u81ea\u9633\u5149\u7535\u8bdd\u8f66\u9669\u7684\uff0c\u5443\uff0c\u4e0910\u7ed9\u60a8\u53d1\u7684\u90a3\u4e2a", "channel_id": 1, "emotion_value": 7.0}]}
text_str type: <class 'str'>

3.str转json报错
JSONDecodeError: Expecting property name enclosed in double quotes
原因以及解答:https://stackoverflow.com/questions/39491420/python-jsonexpecting-property-name-enclosed-in-double-quotes

我输入的string 是:

{'sentences': [{'text': '嗯', 'emotion_value': 7.0, 'silence_duration': 0, 'end_time': 16555, 'channel_id': 1, 'begin_time': 14260, 'speech_rate': 26}, {'text': '嗯', 'emotion_value': 7.0, 'silence_duration': 2, 'end_time': 19495, 'channel_id': 1, 'begin_time': 18730, 'speech_rate': 78}, {'text': '喂', 'emotion_value': 7.0, 'silence_duration': 1, 'end_time': 25595, 'channel_id': 1, 'begin_time': 24620, 'speech_rate': 61}, {'text': '您好,您拨打的电话正在通话中,请稍后再拨the are you help', 'emotion_value': 7.0, 'silence_duration': 0, 'end_time': 33170, 'channel_id': 0, 'begin_time': 26790, 'speech_rate': 338}]}

在这里插入图片描述{ }里面用单引号’ '不是json,用双引号" "才是json
解决方案:
将str里面的单引号用双引号替换,如下:

str = str.replace("\'", "\"")

(亲测有效)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值