【Python】 str型からOrderedDict型に変換する方法
str型からOrderedDict型*1への変換についてまとめました。
1. JSONDecoderを使う場合
from collections import OrderedDict import json decoder = json.JSONDecoder(object_pairs_hook=OrderedDict) json_string = '{"info":{"name":"牛タン", "price":380}}' print(decoder.decode(json_string))
2. 文字列から直接デコードする場合
from collections import OrderedDict import json json_string = '{"info":{"name":"牛タン", "price":380}}' od = json.loads(json_string, object_pairs_hook=OrderedDict) print(od)
参考: python - Converting string to ordered dictionary? - Stack Overflow