3.json
"{\"message\":null,\"result\":{\"data\":[{\"id\":\"1700519714\",\"type\":\"ADD\",\"keyWord\":\"999999\",\"userId\":\"604B868F7C39427AAB651BF2234DE5A6\",\"createTm\":\"2017/07/24\",\"sourceCode\":\"ODR\",\"thisPoint\":\"90\",\"detailDesc\":\"test\",\"curTotalPoint\":\"597\"}],\"totalCount\":1,\"pageSize\":5,\"pageNo\":1,\"totalPage\":1,\"curPoint\":597},\"status\":\"1\",\"success\":true,\"errorcode\":null}"
需要提取 keyWord 999999
import json
f = open('C:/3.json')
a = f.readline()
b = json.loads(a)
d = b['message\\']
print d
f.close()
运行发现
C:\Users\Administrator\Desktop>C:\Users\Administrator\Desktop\json\1\test.py
Traceback (most recent call last):
File "C:\Users\Administrator\Desktop\json\1\test.py", line 5, in <module>
d = b['message\\']
TypeError: string indices must be integers
一步一步排查发现 b 为<type 'unicode'>类型 是不是 b 应该为 dict 类型才能正常输出。请问大牛们这个问题得怎么解决,感激不尽。
"{\"message\":null,\"result\":{\"data\":[{\"id\":\"1700519714\",\"type\":\"ADD\",\"keyWord\":\"999999\",\"userId\":\"604B868F7C39427AAB651BF2234DE5A6\",\"createTm\":\"2017/07/24\",\"sourceCode\":\"ODR\",\"thisPoint\":\"90\",\"detailDesc\":\"test\",\"curTotalPoint\":\"597\"}],\"totalCount\":1,\"pageSize\":5,\"pageNo\":1,\"totalPage\":1,\"curPoint\":597},\"status\":\"1\",\"success\":true,\"errorcode\":null}"
需要提取 keyWord 999999
import json
f = open('C:/3.json')
a = f.readline()
b = json.loads(a)
d = b['message\\']
print d
f.close()
运行发现
C:\Users\Administrator\Desktop>C:\Users\Administrator\Desktop\json\1\test.py
Traceback (most recent call last):
File "C:\Users\Administrator\Desktop\json\1\test.py", line 5, in <module>
d = b['message\\']
TypeError: string indices must be integers
一步一步排查发现 b 为<type 'unicode'>类型 是不是 b 应该为 dict 类型才能正常输出。请问大牛们这个问题得怎么解决,感激不尽。