
1 wenyu1001 Apr 8, 2016 |
2 momo1999 Apr 8, 2016 try 一下能不能 load 咯 |
3 toono Apr 9, 2016 ```python import json def is_json(myjson): try: json_object = json.loads(myjson) except ValueError, e: return False return True ``` 我只是搬运一下上面的 stack overflow 的答案。 如果使用 json.loads() 方法抛出 ValueError 的话即是解析错误。 |
4 toono Apr 9, 2016 ``` python import json def is_json(myjson): try: json_object = json.loads(myjson) except ValueError, e: return False return True ``` 我只是搬运一下上面的 stack overflow 的答案。 如果使用 json.loads() 方法抛出 ValueError 的话即是解析错误。 |
5 qqmishi Apr 9, 2016 json.loads()解析一下就好了 |
6 SoloCompany Apr 9, 2016 其实 如果你的目的不是准确的格式检查 而仅仅是希望数据格式自动适配 并且常用的数据类型都是 object 而不是 array 直接判断一下第一个字符是不是 { 就好了 如果希望自动适配 array 的话,就多判断一下第一个字符是不是 [ 呗 |
7 florije Apr 9, 2016 json.loads('10086') |
8 florije Apr 9, 2016 json loads 之后不异常,看数据是 dict 或者 array 就可以了其实。 |
10 pynix Apr 9, 2016 感觉 xy problem |
11 calease Apr 9, 2016 @florije json 不止 dict 和 array 。 @null0z 如果很多地方要用的话可以用 contextlib.contextmanager wrap 一下。 @ contextmanager def json_deserializer(my_str): try: yield json.loads(my_str) except: print "not valid json" with json_deserializer(my_str) as my_json_object: print "%s is deserialized into %s" % (my_str, my_json_object) |
14 calease Apr 9, 2016 |