【问题标题】:Extract values from String/"Json" [duplicate]从字符串/“Json”中提取值 [重复]
【发布时间】:2019-03-03 23:47:28
【问题描述】:

我有这个字符串,但我需要从字符串/json 中提取特定值。

{"Artigo":{"NrReg":"81846","Cod":"XXX96000051","Desc_Web":"XX 296 00 BLACK","Desc_Agregadora":"XX 296","PVO":"69","NlDes":"3","Cat1_GrpArtigos":"\t2","Cat2_SegMarca":"s\t0","Cat3_SegAcessorios":{},"Cat4_Categoria":{},"Marca":{},"Attr_Sexo":{},"Attr_MatGenerico":{},"Attr_TipoOculo":{},"Profile_V":"1","Profile_S":"0","StockQty":"0","Image":"XXX96000051.jpg","ImageF":{}}}

我在 Python 3.7 中有这段代码从 URL 中提取这个字符串,然后我需要获取一些值,例如从“Cod”是“XXX96000051”

代码:

import requests
import time
import json

url = "http://www.urlsite.test"
data = requests.get(url)
result = data.json()


print(result)
t0 = time.time()

for i in result:
    print(i[0])

print("Total time " + str(time.time() - t0) + " sec")

【问题讨论】:

    标签: python json string list


    【解决方案1】:

    您应该通过键访问字典的值,因此要获取嵌套在键 Artigo 下的键 Cod 的值,您应该使用:

    result['Artigo']['Cod']
    

    【讨论】:

    • 非常感谢,完美,它有效。
    猜你喜欢
    • 2015-12-22
    • 1970-01-01
    • 2020-03-30
    • 2014-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多