python解析json时出现“string indices must be integers”
2018-02-20 23:20
3079 查看
import json,但是出现了一个奇怪的问题:
string indices must be integers
这个错误告诉我,[ ]里面应该是数字而不是字符串,但是dict使用key访问也可以,这种错误感觉解析出来的结果像是一个list。
所以考虑是字符串解析的问题。打印解析的结果,是一长串的字符串,一堆的key挤在一起,value都缺失了。再打印原始的字符串,发现了一些细节,key并没有被双引号包围,类似于'{a:”abcde”,b:”abcde”}’。
好吧,这是一个不符合json规范的字符串,要处理它,可以使用正则表达式转换成规范的key再解析或者使用其他功能更加强大的库——demjson。
pip install demjson,解决。
json的用法:
import json
data = [ { 'a' : 1, 'b' : 2, 'c' : 3, 'd' : 4, 'e' : 5 } ]
json = json.dumps(data)
print json
#结果为:
#[{"a": 1, "c": 3, "b": 2, "e": 5, "d": 4}]
import json
jsonData = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
text = json.loads(jsonData)
print text
#结果为:
#{u'a': 1, u'c': 3, u'b': 2, u'e': 5, u'd': 4}demjson的用法:
import demjson data = [ { 'a' : 1, 'b' : 2, 'c' : 3, 'd' : 4, 'e' : 5 } ] json = demjson.encode(data) print json #结果为: #[{"a":1,"b":2,"c":3,"d":4,"e":5}] import demjson json = '{"a":1,"b":2,"c":3,"d":4,"e":5}'; text = demjson.decode(json) print text #结果为: #{u'a': 1, u'c': 3, u'b': 2, u'e': 5, u'd': 4}
string indices must be integers
这个错误告诉我,[ ]里面应该是数字而不是字符串,但是dict使用key访问也可以,这种错误感觉解析出来的结果像是一个list。
所以考虑是字符串解析的问题。打印解析的结果,是一长串的字符串,一堆的key挤在一起,value都缺失了。再打印原始的字符串,发现了一些细节,key并没有被双引号包围,类似于'{a:”abcde”,b:”abcde”}’。
好吧,这是一个不符合json规范的字符串,要处理它,可以使用正则表达式转换成规范的key再解析或者使用其他功能更加强大的库——demjson。
pip install demjson,解决。
json的用法:
import json
data = [ { 'a' : 1, 'b' : 2, 'c' : 3, 'd' : 4, 'e' : 5 } ]
json = json.dumps(data)
print json
#结果为:
#[{"a": 1, "c": 3, "b": 2, "e": 5, "d": 4}]
import json
jsonData = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
text = json.loads(jsonData)
print text
#结果为:
#{u'a': 1, u'c': 3, u'b': 2, u'e': 5, u'd': 4}demjson的用法:
import demjson data = [ { 'a' : 1, 'b' : 2, 'c' : 3, 'd' : 4, 'e' : 5 } ] json = demjson.encode(data) print json #结果为: #[{"a":1,"b":2,"c":3,"d":4,"e":5}] import demjson json = '{"a":1,"b":2,"c":3,"d":4,"e":5}'; text = demjson.decode(json) print text #结果为: #{u'a': 1, u'c': 3, u'b': 2, u'e': 5, u'd': 4}
import demjson data = [ { 'a' : 1, 'b' : 2, 'c' : 3, 'd' : 4, 'e' : 5 } ] json = demjson.encode(data) print json #结果为: #[{"a":1,"b":2,"c":3,"d":4,"e":5}] import demjson json = '{"a":1,"b":2,"c":3,"d":4,"e":5}'; text = demjson.decode(json) print text #结果为: #{u'a': 1, u'c': 3, u'b': 2, u'e': 5, u'd': 4}
相关文章推荐
- python错误:TypeError: string indices must be integers
- python错误:TypeError: string indices must be integers
- string indices must be integers --GP调用执行Python时报错
- Openstack 12.04 安装后 nova些列命令报错:ERROR: string indices must be integers, not str
- Python读取json文件,并转化为字典进行提取字段(出现索引must be int,not str)解决方案
- TypeError: string indices must be integers, not str
- React 使用fromidable 模块进行文件上传出现First argument must be a string 的解析错误的原因
- Python出现TypeError: file() argument 1 must be encoded string without NULL bytes, not str问题解决
- 读取页面返回字典值提示错误:TypeError: string indices must be integers, not str
- 快速解决Python问题:TypeError: list indices must be integers or slices, not str
- python:PyInstaller打包发布单个文件错误struct.error: argument for 's' must be a string解决
- 错误异常slice indices must be integers or None or have
- Json解析异常处理方式(JSONException: Value of type java.lang.String cannot be converted to JSONObject)
- httpResponse的getEntity为空,获取了这个数据在解析的时候出现了String不能转换成json对象
- python中super出现的TypeError: must be type, not classobj 原因及解决
- python中super()出现的TypeError: must be type, not classobj
- list indices must be integers or slices, not tuple
- JSONObject 解析从网络获取的json数据时,java.lang.String cannot be converted to JSONObject解决办法(编码 UTF-8)
- 使用 python3.6 调用 jieba 库出现的 ValueError: dictionary file dict.txt must be utf-8 错误
- new JSONObject(str)无法解析 报错:org.json.JSONException: Value of type java.lang.String cannot be converted to JSONObject