-1

I downloaded a string from an API as such:

{"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}]}

so that whole thing is a string, but what I need is the stuff after data"which looks like a dictionary.

How can I just get that part of the string and convert it to an actual dictionary?

Liam
  • 1,287
  • 2
  • 9
  • 10

2 Answers2

2

U can use json.loads from python standard libary:

import json
s = '{"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(json.loads(s)['data'])

Optional is to add escape(not necessary in this case):

json_acceptable_string = s.replace("'", "\"")

Return

{'retCode': 1, 'data': [{'TShEquity': 161157756356.52, 'primeOperating': '房地产业务及投资零售业务。', 'secShortName': '万科A', 'equType': '沪深A股', 'exchangeCD': 'XSHE', 'exCountryCD': 'CHN', 'listStatusCD': 'L', 'nonrestFloatShares': 11024120600, 'transCurrCD': 'CNY', 'totalShares': 11039152000, 'secID': '000002.XSHE', 'listDate': '1991-01-29', 'ListSectorCD': 1, 'endDate': '2017-06-30', 'nonrestfloatA': 9709165100, 'ListSector': '主板', 'equTypeCD': 'A', 'ticker': '000002', 'officeAddr': '广东省深圳市盐田区大梅沙环梅路33号万科中心', 'secFullName': '万科企业股份有限公司', 'partyID': 3}], 'retMsg': 'Success'}

If u need extract endDate value from dict then use:

json.loads(s)['data'][0]['endDate']

Convert a String representation of a Dictionary to a dictionary?

Thaian
  • 1,215
  • 2
  • 20
  • 30
  • I tried that, but I need secID, ticker..etc dict, json.loads() only gives data dict as you said. – Liam Sep 05 '17 at 13:40
1

Python has the json module for this purpose

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"])

In Python 2 would print

[{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'}]

And in Python 3 that supports Unicode out of the box it would print

[{'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}]

Note that the order of the elements in the serialized dictionary has changed between the two Python versions, but the dictionary is the same.

To access individual items inside the "data" block use the [0] descriptor to extract the dictionary out of the block and respective descriptor. For example

print(s["data"][0]["secID"])

would print

000002.XSHE
Dima Chubarov
  • 16,199
  • 6
  • 40
  • 76