close

undefined

判斷JSON中是否存在某個鍵值:

Code:

import json
jsonstr = "{\"key\":false,\"Special\":\"Test\"}"
str = json.loads(jsonstr)
if 'Special' in str:
    print(" Contain ")
else:
    print(" Not contain ")

Console:

Contain 

 

Python 讀取 JSON String 時,如果遇到 return loads(fp.read(), 第一個可能就是使用到錯誤的函數:

Code:

import json
jsonstr = "{\"key\":false}"
str = json.load(jsonstr)

錯誤訊息:

    str = json.load(jsonstr)
  File "C:\Python27\lib\json\__init__.py", line 287, in load
    return loads(fp.read(),
AttributeError: 'str' object has no attribute 'read'

 

正確的寫法只是將 load 加上 s,變成 loads:

str = json.loads(jsonstr)

 

 

Reference:

1. http://www.runoob.com/python/python-json.html

 

 

 

arrow
arrow
    全站熱搜

    葛瑞斯肯 發表在 痞客邦 留言(0) 人氣()