简体   繁体   English

python json对象数组不可序列化

[英]python json object array not serializable

I have a class and array in a loop. 我在循环中有一个类和数组。 I'm filling the object and appending it to the array. 我正在填充对象并将其附加到数组中。 How do I pass array data to web side through JSON? 如何通过JSON将数组数据传递到Web端? I have an error that says JSON is not serializable. 我有一个错误,说JSON不可序列化。 How can I serialize to a JSON? 如何序列化为JSON?

class myclass(object):
    def __init__(self,vehicle,mng01):
        self.vehicle = vehicle
        self.vehicle = vehicle

#--Main function--
@subApp.route('/jsontry', method=['POST'])
def data():
    for x in list:
        vehicle_sum.append(myclass( x,str(mng01))

    return json.dumps({'success':'1','vehicle':vehicle_sum})

Does it say myclass object is not JSON serializable? 是否说myclass对象不是JSON可序列化的? This is because there is no way for json.dumps(..) to know how to JSON-serialize your class. 这是因为json.dumps(..)无法知道如何对类进行JSON序列化。 You will need to write your custom encoder to do that . 您需要编写自定义编码器才能执行此操作

Below is a sample implementation. 下面是一个示例实现。 I am sure you can modify it for your use case. 我相信你可以根据你的用例修改它。

import json

class Temp(object):
    def __init__(self, num):
        self.num = num

class CustomEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, Temp):
            return {"num": obj.num}
        #Let the base class handle the problem.
        return json.JSONEncoder.default(self, obj)

obj = [Temp(42), Temp(42)]


print json.dumps(obj, cls=CustomEncoder)

#Output: [{"num": 42}, {"num": 42}]

If you don't want over-complicate stuff, here's what you can do: 如果你不想要过于复杂的东西,这就是你能做的:

all_vehicles = [{"vehicle": x.vehicle} for x in vehicle_sum]
json.dumps({'success':'1','vehicle':all_vehicles)

It doesn't say that JSON is not serializable, it says that your instances of myclass are not JSON serializable. 它并没有说JSON不是可序列化的,它说你的myclass实例不是JSON可序列化的。 Ie they cannot be represented as JSON, because it is really not clear what do you expect as an output. 即它们不能被表示为JSON,因为它真的不清楚你期望什么作为输出。

To find out how to make a class JSON serializable, check this question: How to make a class JSON serializable 要了解如何使类JSON可序列化,请检查以下问题: 如何使类JSON可序列化

In this trivial case, depending on what exactly you're trying to achieve, you might be good with inserting [x.vehicle for x in vehicle_sum] in your JSON. 在这个简单的案例中,根据你想要实现的目标,你可能会在你的JSON中插入[x.vehicle for x in vehicle_sum] If you really need to insert your instances data in JSON in a direct way, you'll have to write an encoder. 如果您确实需要以直接方式在JSON中插入实例数据,则必须编写编码器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError:Python对象不是JSON可序列化的 - TypeError: Python object is not JSON serializable TypeError:array([&#39;cycling&#39;],dtype = object)不可序列化JSON - TypeError: array(['cycling'], dtype=object) is not JSON serializable 类型错误:类型对象<Foo>不是 JSON 可序列化的(在数组中) - TypeError: Object of type <Foo> is not JSON serializable (in Array) Python/Flask - “TypeError”类型的对象不是 JSON 可序列化的 - Python/Flask - Object of type "TypeError' is not JSON serializable 类型为&#39;_NestedList&#39;的python&livejson对象不是JSON可序列化的 - python & livejson Object of type '_NestedList' is not JSON serializable rdd 类型的 object 不是 json 可序列化 python 火花 - object of type rdd is not json serializable python spark Python TypeError:“ ndarray”类型的对象不可JSON序列化 - Python TypeError: Object of type 'ndarray' is not JSON serializable 有没有一种简单的方法来检查对象是否在python中是可序列化的? - Is there an easy way to check if an object is JSON serializable in python? WebElement 类型的 Python Object 不是 JSON 可序列化的 - Python Object of type WebElement is not JSON serializable Python - Flask - TypeError 类型的 Object 不是 JSON 可序列化的 - Python - Flask - Object of type TypeError is not JSON serializable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM