我用opener.open方法得到了json數據。現在我想參考它的elmentS。我嘗試了下面的代碼,但我得到錯誤!此外,我想僅爲鏈接2獲取token =的值。任何人都可以幫助我解決這個錯誤並獲得令牌的價值嗎?提前致謝。如何使用python解析json元素?
代碼:
resp2 = opener.open('http://somewebsite.com/test/id',post_data)
print resp2.read()
Response = resp2.read();
j_obj = json.load(Response)
print j_obj['link2']
錯誤:
ERROR: EXCEPTION Thrown (PythonToCppException) :
-->Python callback/script returned the following error<--
- NOTE: IGNORING THIS CAN LEAD TO MEMORY LEAKS!
Error Type: <type 'exceptions.AttributeError'>
Error Contents: 'str' object has no attribute 'read'
j_obj = json.load(Response)
line 286, in load
return loads(fp.read(),
AttributeError: 'str' object has no attribute 'read'
-->End of Python script error report<--
JSON數據:
{
"id": 1,
"name": "Test World",
"link1": "rtmp:\/\/me.someWebsite.com:1234\/static\/testWorld1?token=123456789abcdefghijklmnopqr&e=987654321&u=99999",
"link2": "http:\/\/me.someWebsite.com:1234\/testWorld1\/index.m3u8?token=123456789abcdefghijklmnopqr&e=987654321&u=99999&channel=testWorld1",
"image": "http:\/\/me.someWebsite.com\/img\/1\/2\/3\/4\/56.png",
"net": "rtmp:\/\/me.someWebSite.com:1234\/static",
"url": "testWorld1?token=123456789abcdefghijklmnopqr&e=987654321&u=99999",
"favorite": false,
"date": "2014-05-1"
}
'json.load()'是文件類型的對象,'json.loads()'是用於字符串。 – AChampion