Last active
August 23, 2017 20:14
-
-
Save aragaer/03f7dcff8eab64277b0d557a2f4ab95c to your computer and use it in GitHub Desktop.
my_json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import json | |
| from io import StringIO | |
| from json.decoder import JSONObject | |
| from json.scanner import py_make_scanner | |
| my_json='''{"ok":true,"result":[{"update_id":547677716, | |
| "message":{"message_id":45,"from":{"id":43543351,"is_bot":false,"first_name":"Aragaer","username":"aragaer","language_code":"en-US"},"chat":{"id":43543351,"first_name":"Aragaer","username":"aragaer","type":"private"},"date":1503481512,"text":"\u0430 \u0432\u043e\u0442 \u043e\u043f\u044f\u0442\u044c"}},{"update_id":547677717, | |
| "message":{"message_id":46,"from":{"id":43543351,"is_bot":false,"first_name":"Aragaer","username":"aragaer","language_code":"en-US"},"chat":{"id":43543351,"first_name":"Aragaer","username":"aragaer","type":"private"},"date":1503481513,"text":"\u0438 \u043e\u043f\u044f\u0442\u044c"}}]}''' | |
| def JSONObject_asStr(s_and_end, strict, scan_once, object_hook, object_pairs_hook, | |
| memo=None, _w=None, _ws=None): | |
| s, pos = s_and_end | |
| output = StringIO() | |
| output.write('{') | |
| count = 1 | |
| in_string = False | |
| while count: | |
| if s[pos] == '\\': | |
| output.write(s[pos:pos+2]) | |
| pos += 2 | |
| continue | |
| if s[pos] == '"': | |
| in_string = not in_string | |
| if not in_string: | |
| if s[pos] == '}': | |
| count -= 1 | |
| elif s[pos] == '{': | |
| count += 1 | |
| output.write(s[pos]) | |
| pos += 1 | |
| return output.getvalue(), pos | |
| _decoder = json.JSONDecoder() | |
| _decoder.parse_object = JSONObject_asStr | |
| _decoder.scan_once = py_make_scanner(_decoder) | |
| def decode_toplevel(text): | |
| return JSONObject((text, 1), True, _decoder.scan_once, None, None)[0] | |
| result = decode_toplevel(my_json) | |
| for item in result['result']: | |
| print(type(item), item) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment