python3 json数据转换之demjson模块

s = '{a:"000001_Unit_1. Hi,Birdie.mp3",b:"000005_Unit_2. Good morning,Miss Wang..mp3",c:"000008_Unit_3. What\'s your name_.mp3"}'

上面数据有没有发现什么问题,json格式的但是,key值都缺少引号。此时用json模块无法解析。

我们需要一个新的模块:demjson模块

# -*- coding: utf-8 -*-

import demjson
s = '{a:"000001_Unit_1. Hi,Birdie.mp3",b:"000005_Unit_2. Good morning,Miss Wang..mp3",c:"000008_Unit_3. What\'s your name_.mp3"}'

data1 = demjson.decode(s)
print(data1)
print(type(data1))

data2 = demjson.encode(data1)
print(data2)
print(type(data2))

运行结果如下:

{'a': '000001_Unit_1. Hi,Birdie.mp3', 'b': '000005_Unit_2. Good morning,Miss Wang..mp3', 'c': "000008_Unit_3. What's your name_.mp3"}

{"a":"000001_Unit_1. Hi,Birdie.mp3","b":"000005_Unit_2. Good morning,Miss Wang..mp3","c":"000008_Unit_3. What's your name_.mp3"}

 

你可能感兴趣的:(python)