simplejson

simplejson.loads() get Invalid \\escape: 'x'

半腔热情 提交于 2019-12-01 17:26:25
I am learning how to use simplejson to decode JSON file. But I suffered the "invalid \escape" error. Here is the code import simplejson as json def main(): json.loads(r'{"test":"\x27"}') if __name__ == '__main__': main() And here is the error message Traceback (most recent call last): File "hello_world.py", line 7, in <module> main() File "hello_world.py", line 4, in main json.loads(r'{"test":"\x27"}') File "C:\Users\zhangkai\python\simplejson\__init__.py", line 307, in loads return _default_decoder.decode(s) File "C:\Users\zhangkai\python\simplejson\decoder.py", line 335, in decode obj, end =

simplejson.loads() get Invalid \escape: 'x'

穿精又带淫゛_ 提交于 2019-12-01 15:45:35
问题 I am learning how to use simplejson to decode JSON file. But I suffered the "invalid \escape" error. Here is the code import simplejson as json def main(): json.loads(r'{"test":"\x27"}') if __name__ == '__main__': main() And here is the error message Traceback (most recent call last): File "hello_world.py", line 7, in <module> main() File "hello_world.py", line 4, in main json.loads(r'{"test":"\x27"}') File "C:\Users\zhangkai\python\simplejson\__init__.py", line 307, in loads return _default

Is there any way to make simplejson less strict?

泄露秘密 提交于 2019-11-30 20:52:53
I'm interested in having simplejson.loads() successfully parse the following: {foo:3} It throws a JSONDecodeError saying "expecting property name" but in reality it's saying "I require double quotes around my property names". This is annoying for my use case, and I'd prefer a less strict behavior. I've read the docs, but beyond making my own decoder class, I don't see anything obvious that changes this behavior. You can use YAML (>=1.2)as it is a superset of JSON, you can do: >>> import yaml >>> s = '{foo: 8}' >>> yaml.load(s) {'foo': 8} You can try demjson . >>> import demjson >>> demjson

How to ensure that a python dict keys are lowercase?

主宰稳场 提交于 2019-11-30 19:42:59
I have a dict that I want to convert in JSON using simplejson. How can I ensure that all the keys of my dict are lowercase ? { "DISTANCE": 17.059918745802999, "name": "Foo Bar", "Restaurant": { "name": "Foo Bar", "full_address": { "country": "France", "street": "Foo Bar", "zip_code": "68190", "city": "UNGERSHEIM" }, "phone": "+33.389624300", "longitude": "7.3064454", "latitude": "47.8769091", "id": "538" }, "price": "", "composition": "", "profils": {}, "type_menu": "", "ID": "" }, EDIT: Thanks all to had a look at my question, I am sorry I didn't explain in detailed why I wanted this. It was

MongoDB Object Serialized as JSON

半城伤御伤魂 提交于 2019-11-30 11:10:02
问题 I'm attempting to send a JSON encoded MongoDB object back in my HTTP response. I've followed several other similar questions but am still missing something. No exceptions are thrown, but I get a cryptic <api.views.MongoEncoder object at 0x80a0c02c> response in the browser. I'm sure it's something simple, but any help would be appreciated. Function: from django.utils.simplejson import JSONEncoder from pymongo.objectid import ObjectId class MongoEncoder( JSONEncoder ): def _iterencode( self, o,

How to make simplejson serializable class

北城余情 提交于 2019-11-30 07:34:08
I have a class defined like this class A: def __init__(self): self.item1 = None def __repr__(self): return str(self.__dict__) when I do: >>> import simplejson >>> myA = A() >>> simplejson.dumps(myA) TypeError: {'item1': None} is not JSON serializable I can't find the reason why. Do I need to add any particular method to A for simplejson to serialize my class object? You can't serialize arbitrary objects with simplejson . You need to pass a default and object_hook to dump and load . Here's an example: class SerializerRegistry(object): def __init__(self): self._classes = {} def add(self, cls):

Is there any way to make simplejson less strict?

别来无恙 提交于 2019-11-30 05:27:28
问题 I'm interested in having simplejson.loads() successfully parse the following: {foo:3} It throws a JSONDecodeError saying "expecting property name" but in reality it's saying "I require double quotes around my property names". This is annoying for my use case, and I'd prefer a less strict behavior. I've read the docs, but beyond making my own decoder class, I don't see anything obvious that changes this behavior. 回答1: You can use YAML (>=1.2)as it is a superset of JSON, you can do: >>> import

How to ensure that a python dict keys are lowercase?

ε祈祈猫儿з 提交于 2019-11-30 02:43:41
问题 I have a dict that I want to convert in JSON using simplejson. How can I ensure that all the keys of my dict are lowercase ? { "DISTANCE": 17.059918745802999, "name": "Foo Bar", "Restaurant": { "name": "Foo Bar", "full_address": { "country": "France", "street": "Foo Bar", "zip_code": "68190", "city": "UNGERSHEIM" }, "phone": "+33.389624300", "longitude": "7.3064454", "latitude": "47.8769091", "id": "538" }, "price": "", "composition": "", "profils": {}, "type_menu": "", "ID": "" }, EDIT:

MongoDB Object Serialized as JSON

巧了我就是萌 提交于 2019-11-29 23:22:46
I'm attempting to send a JSON encoded MongoDB object back in my HTTP response. I've followed several other similar questions but am still missing something. No exceptions are thrown, but I get a cryptic <api.views.MongoEncoder object at 0x80a0c02c> response in the browser. I'm sure it's something simple, but any help would be appreciated. Function: from django.utils.simplejson import JSONEncoder from pymongo.objectid import ObjectId class MongoEncoder( JSONEncoder ): def _iterencode( self, o, markers = None ): if isinstance( o, ObjectId ): return """ObjectId("%s")""" % str(o) else: return

Why does json serialization of datetime objects in python not work out of the box for datetime objects

烂漫一生 提交于 2019-11-29 20:24:32
Why does the json serialization not work for datetime objects . As I understand json serialization the basic idea for any object can be call the __str__ builtin function and then urlencode the object that you get as a response. But in case of datetime i get the following error TypeError: datetime.datetime(2012, 5, 23, 18, 38, 23, 37566) is not JSON serializable while there is a __str__ i.e a way of stringifying the object already available , But it seems like a conscious decision to not do it , why would that be the case? Vikas No it doesn't work that way in json module. The module provides