如何使用json.loads将字符串int JSON转换为真正的int [英] How to convert string int JSON into real int with json.loads
问题描述
我正在尝试使用json.loads
将代表JSON对象的字符串转换为真实的JSON对象,但它不会转换整数:
(在初始字符串中,整数始终是字符串)
$> python
Python 2.7.9 (default, Aug 29 2016, 16:00:38)
[GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import json
>>> c = '{"value": "42"}'
>>> json_object = json.loads(c, parse_int=int)
>>> json_object
{u'value': u'42'}
>>> json_object['value']
u'42'
>>>
我希望它成为
正如我们在注释中所建立的那样,目前没有为您执行此操作的功能.而且我通读了JSONDecoder上的文档和一些示例,而且如果不对数据进行两次处理,它似乎也无法完成您想要的操作.
那么最好的选择就是这样: 这具有两次处理JSON对象的缺点-一次在 要使用它,例如: I'm trying to convert a string which represents a JSON object to a real JSON object using (in the initial string, integers are always strings) Instead of Thanks to Pierce's proposition:
As we established in the comments, there is no existing functionality to do this for you. And I read through the documentation and some examples on the JSONDecoder and it also appears to not do what you want without processing the data twice. The best option, then, is something like this: This has the downside of processing the JSON object twice — once in the To use it, you do e.g.:
这篇关于如何使用json.loads将字符串int JSON转换为真正的int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!{u'value': 42}
,而不是{u'value': u'42'}
.我知道我可以遍历整个对象,但是我不想这样做,因为存在此parse_int
参数(class Decoder(json.JSONDecoder):
def decode(self, s):
result = super().decode(s) # result = super(Decoder, self).decode(s) for Python 2.x
return self._decode(result)
def _decode(self, o):
if isinstance(o, str) or isinstance(o, unicode):
try:
return int(o)
except ValueError:
return o
elif isinstance(o, dict):
return {k: self._decode(v) for k, v in o.items()}
elif isinstance(o, list):
return [self._decode(v) for v in o]
else:
return o
super().decode(s)
调用中,再一次遍历整个结构以修复问题.另请注意,这会将所有看起来像整数的东西转换为int
.请务必对此进行适当说明.>>> c = '{"value": "42"}'
>>> json.loads(c, cls=Decoder)
{'value': 42}
json.loads
but it doesn't convert the integers:$> python
Python 2.7.9 (default, Aug 29 2016, 16:00:38)
[GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import json
>>> c = '{"value": "42"}'
>>> json_object = json.loads(c, parse_int=int)
>>> json_object
{u'value': u'42'}
>>> json_object['value']
u'42'
>>>
{u'value': u'42'}
I'd like it becomes {u'value': 42}
. I know I can run through the whole object, but I don't want to do that, it's not really efficient to do it manually, since this parse_int
argument exists (https://docs.python.org/2/library/json.html#json.loads). Python 2.7.9 (default, Aug 29 2016, 16:00:38)
[GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import json
>>>
>>> class Decoder(json.JSONDecoder):
... def decode(self, s):
... result = super(Decoder, self).decode(s)
... return self._decode(result)
... def _decode(self, o):
... if isinstance(o, str) or isinstance(o, unicode):
... try:
... return int(o)
... except ValueError:
... try:
... return float(o)
... except ValueError:
... return o
... elif isinstance(o, dict):
... return {k: self._decode(v) for k, v in o.items()}
... elif isinstance(o, list):
... return [self._decode(v) for v in o]
... else:
... return o
...
>>>
>>> c = '{"value": "42", "test": "lolol", "abc": "43.4", "dcf": 12, "xdf": 12.4}'
>>> json.loads(c, cls=Decoder)
{u'test': u'lolol', u'dcf': 12, u'abc': 43.4, u'value': 42, u'xdf': 12.4}
class Decoder(json.JSONDecoder):
def decode(self, s):
result = super().decode(s) # result = super(Decoder, self).decode(s) for Python 2.x
return self._decode(result)
def _decode(self, o):
if isinstance(o, str) or isinstance(o, unicode):
try:
return int(o)
except ValueError:
return o
elif isinstance(o, dict):
return {k: self._decode(v) for k, v in o.items()}
elif isinstance(o, list):
return [self._decode(v) for v in o]
else:
return o
super().decode(s)
call, and again to recurse through the entire structure to fix things. Also note that this will convert anything which looks like an integer into an int
. Be sure to account for this appropriately.>>> c = '{"value": "42"}'
>>> json.loads(c, cls=Decoder)
{'value': 42}