如何使用json.loads将string int JSON转换为real int

Pie*_*rre 3 python parsing json python-2.7

我正在尝试将表示JSON对象的字符串转换为真正的JSON对象,json.loads但它不会转换整数:

(在初始字符串中,整数总是字符串)

$> 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'
>>>
Run Code Online (Sandbox Code Playgroud)

而不是{u'value': u'42'}我喜欢它成为{u'value': 42}.我知道我可以浏览整个对象,但我不想这样做,手动执行它并不是很有效,因为这个parse_int参数存在(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}
Run Code Online (Sandbox Code Playgroud)

jua*_*nra 8

除了 Pierce 响应之外,我认为您可以使用 json.loadsobject_hook参数代替cls一个,因此您不需要两次遍历 json 对象。

例如:

def _decode(o):
    # Note the "unicode" part is only for python2
    if isinstance(o, str) or isinstance(o, unicode):
        try:
            return int(o)
        except ValueError:
            return o
    elif isinstance(o, dict):
        return {k: _decode(v) for k, v in o.items()}
    elif isinstance(o, list):
        return [_decode(v) for v in o]
    else:
        return o

# Then you can do:
json.loads(c, object_hook=_decode)
Run Code Online (Sandbox Code Playgroud)

正如@ZhanwenChen 在评论中指出的那样,上面的代码是针对python2 的。对于python3,您需要删除or isinstance(o, unicode)第一个if条件中的部分。


Pie*_*agh 6

正如我们在评论中所建立的那样,没有现成的功能可以帮助您.我阅读了JSONDecoder上的文档和一些示例,如果不对数据进行两次处理,它似乎也没有做到你想做的事情.

那么,最好的选择是这样的:

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
Run Code Online (Sandbox Code Playgroud)

这有两个处理JSON对象的缺点 - 一次在super().decode(s)调用中,并再次递归整个结构来修复事情.另请注意,这会将看起来像整数的任何内容转换为int.一定要适当考虑到这一点.

要使用它,您可以:

>>> c = '{"value": "42"}'
>>> json.loads(c, cls=Decoder)
{'value': 42}
Run Code Online (Sandbox Code Playgroud)