将 Python 字典转换为 JSON 数组 [英] Convert Python dictionary to JSON array

查看:81
本文介绍了将 Python 字典转换为 JSON 数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我有这本字典,使用 pprint 打印:

{'AlarmExTempHum': '\x00\x00\x00\x00\x00\x00\x00\x00','报警输入':0,'报警输出': '\x00\x00','警报雨':0,'报警土壤叶': '\x00\x00\x00\x00','酒吧趋势':60,'电池状态':0,电池电压":4.751953125,'CRC': 55003,'EOL': '\n\r','ETDay': 0,'ETMonth': 0,'ETYear': 0,'ExtraHum1':无,'ExtraHum2':无,'ExtraHum3':无,'ExtraHum4':无,'ExtraHum5':无,'ExtraHum6':无,'ExtraHum7':无,'ExtraTemp1':无,'ExtraTemp2':无,'ExtraTemp3':无,'ExtraTemp4':无,'ExtraTemp5':无,'ExtraTemp6':无,'ExtraTemp7':无,'预测图标':2,'ForecastRuleNo': 122,'嗡嗡声':31,'HumOut':94,'LOO': 'LOO','LeafTemps': '\xff\xff\xff\xff','LeafWetness': '\xff\xff\xff\x00','NextRec': 37,'数据包类型':0,'压力':995.9363359295631,'雨天':0.0,'雨月':0.0,'降雨率':0.0,'暴雨':0.0,'雨年':2.8,'SoilMoist': '\xff\xff\xff\xff','SoilTemps': '\xff\xff\xff\xff','SolarRad':无,'风暴开始日期':'2127-15-31','日出':849,日落":1611,'TempIn':21.38888888888889,'TempOut':0.88888888888888897,'紫外线':无,'风向':219,'风速':3.6,'风速10分钟':3.6}

当我这样做时:

导入jsond =(我上面的字典)jsonarray = json.dumps(d)

我收到这个错误:'utf8' codec can't decode byte 0xff in position 0: invalid start byte

解决方案

如果您对 json 中不可打印的符号感到满意,那么将 ensure_ascii=False 添加到 dumps> 打电话.

<预><代码>>>>json.dumps(your_data, ensure_ascii=False)

<块引用>

如果 ensure_ascii 为假,则返回值将是一个unicode 实例服从普通 Python strunicode强制规则而不是转义为 ASCII str.

Currently I have this dictionary, printed using pprint:

{'AlarmExTempHum': '\x00\x00\x00\x00\x00\x00\x00\x00',  
'AlarmIn': 0,  
'AlarmOut': '\x00\x00',  
'AlarmRain': 0,  
'AlarmSoilLeaf': '\x00\x00\x00\x00',  
'BarTrend': 60,  
'BatteryStatus': 0,  
'BatteryVolts': 4.751953125,  
'CRC': 55003,
'EOL': '\n\r',
'ETDay': 0,
'ETMonth': 0,
'ETYear': 0,
'ExtraHum1': None,
'ExtraHum2': None,
'ExtraHum3': None,
'ExtraHum4': None,
'ExtraHum5': None,
'ExtraHum6': None,
'ExtraHum7': None,
'ExtraTemp1': None,
'ExtraTemp2': None,
'ExtraTemp3': None,
'ExtraTemp4': None,
'ExtraTemp5': None,
'ExtraTemp6': None,
'ExtraTemp7': None,
'ForecastIcon': 2,
'ForecastRuleNo': 122,
'HumIn': 31,
'HumOut': 94,
'LOO': 'LOO',
'LeafTemps': '\xff\xff\xff\xff',
'LeafWetness': '\xff\xff\xff\x00',
'NextRec': 37,
'PacketType': 0,
'Pressure': 995.9363359295631,
'RainDay': 0.0,
'RainMonth': 0.0,
'RainRate': 0.0,
'RainStorm': 0.0,
'RainYear': 2.8,
'SoilMoist': '\xff\xff\xff\xff',
'SoilTemps': '\xff\xff\xff\xff',
'SolarRad': None,
'StormStartDate': '2127-15-31',
'SunRise': 849,
'SunSet': 1611,
'TempIn': 21.38888888888889,
'TempOut': 0.8888888888888897,
'UV': None,
'WindDir': 219,
'WindSpeed': 3.6,
'WindSpeed10Min': 3.6}

When I do this:

import json
d = (my dictionary above)
jsonarray = json.dumps(d)

I get this error: 'utf8' codec can't decode byte 0xff in position 0: invalid start byte

解决方案

If you are fine with non-printable symbols in your json, then add ensure_ascii=False to dumps call.

>>> json.dumps(your_data, ensure_ascii=False)

If ensure_ascii is false, then the return value will be a unicode instance subject to normal Python str to unicode coercion rules instead of being escaped to an ASCII str.

这篇关于将 Python 字典转换为 JSON 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆