简体   繁体   中英

TypeError: string indices must be integers in json

I have a dictionary that contains a list like this

data = {'data': [18,170,457,99,40,0,0,4,
  434,168,454,99,40,3,1,
  0,  421,168,452,100,41,2,
  0,4,455,13,32,11,6,
  0,0,4,279,14,46,10,5,
  0,0,1,257,13,44,10,5,
  1,1,254,0]}

When I am trying to convert this into json, it is giving me an error

json.dumps(data)['data']

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-94-ca909e16d3cc> in <module>
      6   0,0,1,257,13,44,10,5,
      7   1,1,254,0]}
----> 8 json.dumps(data)['data']

TypeError: string indices must be integers

如果要将 JSON 字符串转换回必须使用的字典,则将字典转换为字符串json.loads(json_string) JSON 对象几乎在每种语言中都是实际 Javascript 对象的字符串表示形式 您可以将其用作Python 中的一个 dict 但如果你想让它导出它你需要把它串起来

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM