簡體   English   中英

如何使用python提取JSON?

[英]How to extract JSON using python?

我正在嘗試提取一個 json。 我為“錯誤”提取了錯誤的數據。 當我嘗試使用t3 = temp['errors'][0]我只得到"Arg one must not be null or empty."

預期輸出:

"Arg one must not be null or empty.",
"Arg two must not be null or empty."

這是我的json:

{
        "status": "Fail",
        "warnings": {
            "Code": "VALID",
            "Desc": "Invalid data",
            "errors": [
                "Arg one must not be null or empty.",
                "Arg two must not be null or empty."

            ]
        }
    }

這是我的代碼:

tmp = json.loads(res.content)
print(tmp['status'])
temp = (tmp['warnings'])
t1 = temp['errorCode']
t2 = temp['errorDesc']
t3 = temp['errors'][0]
print(t1)
print(t2)
print(t3)

有人請糾正我我做錯了什么?

試試吧:

t3 = temp['errors']
t3
# ['Arg one must not be null or empty.', 
# 'Arg two must not be null or empty.']

你得到括號,因為它是一個列表,如果你想將兩者連接成一個字符串,你可以這樣做:

', '.join( temp['errors'] )
# 'Arg one must not be null or empty., Arg two must not be null or empty.'

這將創建一個內容字符串,沒有括號

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM