JSON與Python資料類型關係表 === ###### tags: `Python` `JSON` JSON是現有REST API的主流形式,但是兩者之間的資料類型有些許差異必須注意。 最典型會遇到的情形就是JSON中的true與false來到Python必須轉換為True與False才能正常判斷 下列是兩者資料型態的關係表 | Python | JSON | | -------- | -------- | | dict | object | | list, tuple | array | | str, unicode | string | | int, long, float | number | | True | true | | False | false | | None | null | json的編碼和解碼在python 中可以透過json套件完成。 ```= import json #必須先導入json函式庫 # json字串轉python dict str = '{"id": 123, "Name": "howard"}' json.loads(str) # python dict包含list轉json字串 j = {"id":[1,2,3], "name": ["Tim", "Bob", "Lucy"]} json.dumps(j) ```