Python解析json时提示“string indices must be integers”问题解决方法
本文实例讲述了Python解析json时提示“string indices must be integers"问题解决方法。分享给大家供大家参考,具体如下:
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}
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}
PS:这里再为大家推荐几款比较实用的json在线工具供大家参考使用:
在线JSON代码检验、检验、美化、格式化工具:
http://tools.jb51.net/code/json
JSON在线格式化工具:
http://tools.jb51.net/code/jsonformat
在线XML/JSON互相转换工具:
http://tools.jb51.net/code/xmljson
json代码在线格式化/美化/压缩/编辑/转换工具:
http://tools.jb51.net/code/jsoncodeformat
在线json压缩/转义工具:
http://tools.jb51.net/code/json_yasuo_trans
更多Python相关内容感兴趣的读者可查看本站专题:《Python操作json技巧总结》、《Python编码操作技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》
希望本文所述对大家Python程序设计有所帮助。
您可能感兴趣的文章:
- python解析json时出现“string indices must be integers”
- Python BeautifulSoup [解决方法] TypeError: list indices must be integers or slices, not str
- 快速解决Python问题:TypeError: list indices must be integers or slices, not str
- Python中json.loads()无法解析单引号字符串问题的两种解决方法
- Python实现json解析中的问题及解决方法
- python错误:TypeError: string indices must be integers
- 【新手上路】-【flask项目】解决return binds[self._bind] 报TypeError: string indices must be integers错误
- 【python tip】TypeError: list indices must be integers or slices, not tuple解决
- 读取页面返回字典值提示错误:TypeError: string indices must be integers, not str
- Python出现TypeError: file() argument 1 must be encoded string without NULL bytes, not str问题解决
- python错误:TypeError: string indices must be integers
- python错误: TypeError: string indices must be integers
- A context path must either be an empty string or start with a '/' and do not end with a '/' 问题解决方法
- which must be escaped when used within the value 问题解决方法
- c++中调用python脚本提示 error LNK2001: 无法解析的外部符号 __imp_Py_Initialize等错误的解决方法
- 使用Gson解析Json数组遇到的泛型类型擦除问题解决方法
- c++中调用python脚本提示 error LNK2001: 无法解析的外部符号 __imp_Py_Initialize等错误的解决方法
- TypeError: string indices must be integers, not str
- Python EOL while scanning string literal问题解决方法
- 在线聊天项目1.4版 使用Gson方法解析Json字符串以便重构request和response的各种请求和响应 解决聊天不畅问题 Gson包下载地址