Python有用於此目的的json
模塊
import json
s = json.loads('{"retCode":1,"retMsg":"Success","data":[{"secID":"000002.XSHE","ticker":"000002","exchangeCD":"XSHE","ListSectorCD":1,"ListSector":"主板","transCurrCD":"CNY","secShortName":"萬科A","secFullName":"萬科企業股份有限公司","listStatusCD":"L","listDate":"1991-01-29","equTypeCD":"A","equType":"滬深A股","exCountryCD":"CHN","partyID":3,"totalShares":11039152000,"nonrestFloatShares":11024120600,"nonrestfloatA":9709165100,"officeAddr":"廣東省深圳市鹽田區大梅沙環梅路33號萬科中心","primeOperating":"房地產業務及投資零售業務。","endDate":"2017-06-30","TShEquity":161157756356.52}]}')
print(s["data"])
在Python 2將打印
[{u'primeOperating': u'\u623f\u5730\u4ea7\u4e1a\u52a1\u53ca\u6295\u8d44\u96f6\u552e\u4e1a\u52a1\u3002', u'ListSectorCD': 1, u'exchangeCD': u'XSHE', u'secID': u'000002.XSHE', u'secFullName': u'\u4e07\u79d1\u4f01\u4e1a\u80a1\u4efd\u6709\u9650\u516c\u53f8', u'nonrestFloatShares': 11024120600, u'endDate': u'2017-06-30', u'officeAddr': u'\u5e7f\u4e1c\u7701\u6df1\u5733\u5e02\u76d0\u7530\u533a\u5927\u6885\u6c99\u73af\u6885\u8def33\u53f7\u4e07\u79d1\u4e2d\u5fc3', u'listDate': u'1991-01-29', u'secShortName': u'\u4e07\u79d1A', u'TShEquity': 161157756356.52, u'equType': u'\u6caa\u6df1A\u80a1', u'nonrestfloatA': 9709165100, u'listStatusCD': u'L', u'ListSector': u'\u4e3b\u677f', u'partyID': 3, u'totalShares': 11039152000, u'transCurrCD': u'CNY', u'exCountryCD': u'CHN', u'ticker': u'000002', u'equTypeCD': u'A'}]
而且在Python 3支持Unicode開箱,將打印
[{'nonrestfloatA': 9709165100, 'listDate': '1991-01-29', 'officeAddr': '廣東省深圳市鹽田區大 梅沙環梅路33號萬科中心', 'ListSectorCD': 1, 'secShortName': '萬科A', 'primeOperating': '房地產業務及投資零售業務。', 'secFullName': '萬科企業股份有限公司', 'TShEquity': 161157756356.52, 'exCountryCD': 'CHN', 'nonrestFloatShares': 11024120600, 'exchangeCD': 'XSHE', 'equType': '滬深A 股', 'secID': '000002.XSHE', 'ticker': '000002', 'endDate': '2017-06-30', 'transCurrCD': 'CNY', 'ListSector': '主板', 'equTypeCD': 'A', 'partyID': 3, 'listStatusCD': 'L', 'totalShares': 11039152000}]
請注意,序列化字典中元素的順序在兩個Python版本之間已更改,但字典相同。
要訪問「數據」塊內的各個項目,請使用[0]
描述符從塊和各自的描述符中提取字典。例如
print(s["data"][0]["secID"])
將打印
000002.XSHE
我試過了,但我需要secID,ticker..etc快譯通,json.loads()只給出了數據字典如你所說。 – Liam