【问题标题】:Unable to serialise python list to json无法将 python 列表序列化为 json
【发布时间】:2020-03-15 22:45:15
【问题描述】:
[ 
   { 
      'topleft':{ 
         'x':34,
         'y':13
      },
      'confidence':0.17681329,
      'bottomright':{ 
         'x':398,
         'y':347
      },
      'label':'person'
   },
   { 
      'topleft':{ 
         'x':29,
         'y':48
      },
      'confidence':0.107129775,
      'bottomright':{ 
         'x':399,
         'y':351
      },
      'label':'car'
   },
   { 
      'topleft':{ 
         'x':20,
         'y':85
      },
      'confidence':0.22963998,
      'bottomright':{ 
         'x':376,
         'y':350
      },
      'label':'cat'
   },
   { 
      'topleft':{ 
         'x':0,
         'y':2
      },
      'confidence':0.12423642,
      'bottomright':{ 
         'x':372,
         'y':356
      },
      'label':'sheep'
   },
   { 
      'topleft':{ 
         'x':20,
         'y':12
      },
      'confidence':0.26517922,
      'bottomright':{ 
         'x':378,
         'y':349
      },
      'label':'dog'
   }
]

这是从 TensorFlow 返回的对象数组。但是,在尝试使用 json.dumps 转换为 JSON 对象时,我收到了此错误 TypeError: 0.17681329 is not JSON serializable

我尝试使用 simplejson 并得到 Float32 不可序列化。如何将此对象数组转换为 JSON 可序列化?

【问题讨论】:

  • 看起来像这样:Convert numpy type to python。您的 float 值(大概)是 numpy float 或特定于 tensorflow 的类型 float,而不是 python float。
  • 可能是那个或小数:在我的回答中是一种将它们转换为浮点数的方法。
  • 如果答案有帮助,请考虑将其标记为正确。

标签: python json


【解决方案1】:

为我工作:

Python 2.7.15+ (default, Oct  7 2019, 17:39:04) 
[GCC 7.4.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a = [{'topleft': {'x': 34, 'y': 13}, 'confidence': 0.17681329, 'bottomright': {'x': 398, 'y': 347}, 'label': 'person'}, {'topleft': {'x': 29, 'y': 48}, 'confidence': 0.107129775, 'bottomright': {'x': 399, 'y': 351}, 'label': 'car'}, {'topleft': {'x': 20, 'y': 85}, 'confidence': 0.22963998, 'bottomright': {'x': 376, 'y': 350}, 'label': 'cat'}, {'topleft': {'x': 0, 'y': 2}, 'confidence': 0.12423642, 'bottomright': {'x': 372, 'y': 356}, 'label': 'sheep'}, {'topleft': {'x': 20, 'y': 12}, 'confidence': 0.26517922, 'bottomright': {'x': 378, 'y': 349}, 'label': 'dog'}]
>>> import json
>>> print json.dumps(a, indent=4)
[
    {
        "topleft": {
            "y": 13, 
            "x": 34
        }, 
        "confidence": 0.17681329, 
        "bottomright": {
            "y": 347, 
            "x": 398
        }, 
        "label": "person"
    }, 
    {
        "topleft": {
            "y": 48, 
            "x": 29
        }, 
        "confidence": 0.107129775, 
        "bottomright": {
            "y": 351, 
            "x": 399
        }, 
        "label": "car"
    }, 
    {
        "topleft": {
            "y": 85, 
            "x": 20
        }, 
        "confidence": 0.22963998, 
        "bottomright": {
            "y": 350, 
            "x": 376
        }, 
        "label": "cat"
    }, 
    {
        "topleft": {
            "y": 2, 
            "x": 0
        }, 
        "confidence": 0.12423642, 
        "bottomright": {
            "y": 356, 
            "x": 372
        }, 
        "label": "sheep"
    }, 
    {
        "topleft": {
            "y": 12, 
            "x": 20
        }, 
        "confidence": 0.26517922, 
        "bottomright": {
            "y": 349, 
            "x": 378
        }, 
        "label": "dog"
    }
]

【讨论】:

    【解决方案2】:

    您有一个Decimal 或一些numpy 类型,需要转换为float

    import json
    from decimal import Decimal
    
    import numpy
    
    
    class CustomJsonEncoder(json.JSONEncoder):
    
        def default(self, obj):
            if isinstance(obj, Decimal):
                return float(obj)
            elif isinstance(obj, numpy.integer):
                return int(obj)
            elif isinstance(obj, numpy.floating):
                return float(obj)
            elif isinstance(obj, numpy.ndarray):
                return obj.tolist()
            return super(CustomJsonEncoder, self).default(obj)
    

    然后拨打dumps如下:

    json.dumps(data, cls=CustomJsonEncoder)
    

    【讨论】:

    • 你怎么知道值是浮点类型而不是十进制?尝试将其中一些浮点数转换为十进制然后转储,我得到一个类型错误
    • 嗯,这可能是真的。
    • 很好的答案!你能不能也看看this
    猜你喜欢
    • 2017-09-26
    • 1970-01-01
    • 2014-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-25
    • 1970-01-01
    相关资源
    最近更新 更多