Python解析json

来源:互联网 发布:无限极植雅牙膏知乎 编辑:程序博客网 时间:2024/06/08 00:43

Python默认带有json解析库

import json

原来python需要用反斜杠(\)来续行,开始以为没有(如果没有的话真的是一个折磨,对于强迫症来说),

总的来说python解析json规规矩矩,有一个地方受不了的就是类型转换,真是相当不简洁,代码如下

import jsonfrom urllib.request import urlopenresult = urlopen("http://f.apiplus.cn/cqssc.json").read().decode("utf-8")rj = json.loads(result)temp = "info:" + rj["info"] + "\n"\ + "code:" + str(rj["code"]) + "\n"\ + "rows:" + str(rj["rows"] ) + "\n"for infoitem in rj["data"]:    temp += "    " + "expect:" + str(infoitem.get("expect") ) + "\n"\+ "    " + "opencode:" + infoitem.get("opencode") + "\n"\+ "    " + "opentime:" + infoitem.get("opentime") + "\n"\+ "    " + "opentimestamp:" + str(infoitem.get("opentimestamp") ) + "\n\n"print(temp)

关于类型的强制转换,强类型语言得优势就很明显了,一眼就能看出变量的类型,python这种弱类型语言就有点坑了,而有时候你必须踩坑才知道。
0 0
原创粉丝点击