Python 解释这个 json

Python Interpret this json

我正在使用以下代码:

import json
full_json = "{'Nike - Hombres Tenis Fila': ['/nike/casual/', '100', '/medias/']}"
jtopy=json.dumps(full_json) #json.dumps take a dictionary as input and returns a string as output.
dict_json=json.loads(jtopy) 

print(dict_json['Nike - Hombres Tenis Fila'][0])

如何获取值: /nike/casual/runners/'

我一直收到错误消息:

TypeError: string indices must be integers

您的 json 无效,您需要交换单引号和双引号。
你也想要 json.loads 而不是 json.dumps

>>> import json
>>> full_json = '{"Nike - Hombres Tenis Fila": ["/nike/casual/", "100", "/medias/"]}'
>>> loaded_json = json.loads(full_json)
>>> loaded_json
{'Nike - Hombres Tenis Fila': ['/nike/casual/', '100', '/medias/']}
>>> loaded_json['Nike - Hombres Tenis Fila'][0]
'/nike/casual/'

Json 不接受其字符串中的单引号。要进行查询,您应该:

  1. 将您的 json 字符串替换为此 '{"Nike - Hombres Tenis Fila": ["/nike/casual/", "100", "/medias/"]}'
  2. 运行一个json.loadsdictjson = json.loads('{"Nike - Hombres Tenis Fila": ["/nike/casual/", "100", "/medias/"]}')
  3. 查询dictjson["Nike - Hombres Tenis Fila"][0]