JSON.パース
Python
py
json.loads(text, object_hook=ascii_encode_dict)
object_hook=ascii_encode_dict
のところ、
これやらないと日本語入るとエラーになるとかだっけ?
py
def ascii_encode_dict(data):
ascii_encode = lambda x: x.encode('ascii') if isinstance(x, unicode) else x
return dict(map(ascii_encode, pair) for pair in data.items())
つい でに、jsonc
風のファイルも読みたかったので、
py
def remove_comment(text):
_text = text
_text = re.sub(r'//.*$\n' , '' , _text)
_text = re.sub(r'/\*.*?\*/' , '' , _text, flags = re.MULTILINE | re.DOTALL)
_text = re.sub(r',([\s\n]*)(\]|\})' , r'\1\2' , _text, flags = re.MULTILINE)
PHP
php
json_decode($text)
PowerShell
powershell
$json = (Get-Content "file1.json" | ConvertFrom-Json)