I was trying to serialize a Python list but got errors that it's not serializable. Is there a limitation on serializing a list of Long integers?
我试图序列化一个Python列表,但有一些错误是不能串行化的。对长整数列表序列化有限制吗?
>>> ids=p.values_list('id',flat=True)
>>> ids
[335L, 468L, 481L, 542L, 559L, 567L, 609L]
>>> import simplejson as json
>>> str=json.dumps(ids)
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\Program Files\Google\google_appengine\lib\simplejson\simplejson\__ini
t__.py", line 265, in dumps
return _default_encoder.encode(obj)
File "C:\Program Files\Google\google_appengine\lib\simplejson\simplejson\encod
er.py", line 216, in encode
chunks = list(chunks)
File "C:\Program Files\Google\google_appengine\lib\simplejson\simplejson\encod
er.py", line 495, in _iterencode
o = _default(o)
File "C:\Program Files\Google\google_appengine\lib\simplejson\simplejson\encod
er.py", line 190, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: [335L, 468L, 481L, 542L, 559L, 567L, 609L] is not JSON serializable
>>>
4 个解决方案
#1
14
"I am trying to serialize a Python list..."
This is actually not quite the full story.
You are trying to serialize a ValuesListQuerySet
.
“我正在尝试序列化一个Python列表……”这并不是全部。您正在尝试序列化一个ValuesListQuerySet。
>>> type(ids)
<class 'django.db.models.query.ValuesListQuerySet'>
You can either
1. convert to a Python list as mentioned in the other great answers, or
2. serialize just the IDs.
你可以1。转换为Python列表,如其他优秀答案中提到的,或2。序列化的id。
Django has a built-in way to serialize a QuerySet
.
And you only want the IDs so you may use the fields
kwarg.
Django有一种内置的方法来序列化一个QuerySet。你只需要id,所以你可以用kwarg。
from django.core import serializers
data = serializers.serialize('json', YourEntity.objects.all(), fields=('id',))
#2
4
EDIT after reading the other answers
The original answer I gave (here below unabridged) was correct in diagnosing what the problem is (the argument passed to the json function is not a list). I am leaving it as it explains the debugging procedure (maybe of some use for other similar situations), but the new answers of both @Jacinda and @Adam are more "to the point". In particular, the latter contains instructions on how to use a native django functionality to overcome the problem.
我给出的原始答案(如下未删节)在诊断问题时是正确的(传递给json函数的参数不是列表)。我将它作为调试过程的解释(可能在其他类似的情况下会有一些用处),但是@Jacinda和@Adam的新答案更“切中要点”。特别是,后者包含如何使用本机django功能来解决问题的指令。
Original answer
Not 100% sure because I can't replicate the problem on my system, but from the look of it, it seems to me that it's a problem in the type/encoding of the data.
不是100%确定,因为我不能在我的系统上复制问题,但是从它的外观来看,在我看来,这是数据的类型/编码的问题。
I would start by testing again your code by manually assign ids
with:
我将首先通过手工分配id来再次测试您的代码:
ids = [335L, 468L, 481L, 542L, 559L, 567L, 609L]
(on my system your code works in this case). If it works for you too, then investigate what kind of object is ids
when assigned via p.values_list('id',flat=True)
[you can do that with type(ids)
]. It might be that ids
is an object whose representation is the same as a list, but that it's not a list.
(在我的系统中,您的代码在这种情况下可以工作)。如果它也适用于您,那么当通过p.values_list('id',flat=True)分配id时,研究一下id是什么类型的对象(id)]。可能是id是一个对象,其表示形式与列表相同,但它不是列表。
In this case, you could try typecasting: ids = list(p.values_list('id',flat=True))
before passing it to the json function but there is no guarantee that it will work (it depends if the returned value of p.values_list
is iterable or not.
在本例中,您可以在将其传递给json函数之前尝试typecasting: ids = list(p.values_list('id',flat=True),但不能保证它会工作(这取决于p的返回值)。values_list是否可迭代。
HTH in at least tracking down the issue!
至少追踪这个问题!
#3
3
I encountered the same error and after a lot of confusion, finally the solution that worked for me is as follows in a single answer for the question asked by @afshin:
我遇到了同样的错误,在经历了很多困惑之后,最终对我起作用的解决方案是@afshin所问问题的单一答案:
ids = p.values_list('id',flat=True)
ids_list = list(ids)
import json
json.dumps(ids_list)
#4
1
The problem here is that values_list()
, which is a django function (you probably should clarify this in your question) does not return a list.
这里的问题是values_list(),它是一个django函数(您可能应该在问题中对此进行澄清),它不返回一个列表。
>>> x = UserProfile.objects.values_list('employee_id', flat=True)
>>> type(x)
>>> <class 'django.db.models.query.ValuesListQuerySet'>
simplejson.dumps doesn't know how to serialize this object. So like @mac suggested, you need to typecast this return value to a list.
simplejson。转储不知道如何序列化这个对象。如@mac所建议的,您需要将此返回值输入到列表中。
#1
14
"I am trying to serialize a Python list..."
This is actually not quite the full story.
You are trying to serialize a ValuesListQuerySet
.
“我正在尝试序列化一个Python列表……”这并不是全部。您正在尝试序列化一个ValuesListQuerySet。
>>> type(ids)
<class 'django.db.models.query.ValuesListQuerySet'>
You can either
1. convert to a Python list as mentioned in the other great answers, or
2. serialize just the IDs.
你可以1。转换为Python列表,如其他优秀答案中提到的,或2。序列化的id。
Django has a built-in way to serialize a QuerySet
.
And you only want the IDs so you may use the fields
kwarg.
Django有一种内置的方法来序列化一个QuerySet。你只需要id,所以你可以用kwarg。
from django.core import serializers
data = serializers.serialize('json', YourEntity.objects.all(), fields=('id',))
#2
4
EDIT after reading the other answers
The original answer I gave (here below unabridged) was correct in diagnosing what the problem is (the argument passed to the json function is not a list). I am leaving it as it explains the debugging procedure (maybe of some use for other similar situations), but the new answers of both @Jacinda and @Adam are more "to the point". In particular, the latter contains instructions on how to use a native django functionality to overcome the problem.
我给出的原始答案(如下未删节)在诊断问题时是正确的(传递给json函数的参数不是列表)。我将它作为调试过程的解释(可能在其他类似的情况下会有一些用处),但是@Jacinda和@Adam的新答案更“切中要点”。特别是,后者包含如何使用本机django功能来解决问题的指令。
Original answer
Not 100% sure because I can't replicate the problem on my system, but from the look of it, it seems to me that it's a problem in the type/encoding of the data.
不是100%确定,因为我不能在我的系统上复制问题,但是从它的外观来看,在我看来,这是数据的类型/编码的问题。
I would start by testing again your code by manually assign ids
with:
我将首先通过手工分配id来再次测试您的代码:
ids = [335L, 468L, 481L, 542L, 559L, 567L, 609L]
(on my system your code works in this case). If it works for you too, then investigate what kind of object is ids
when assigned via p.values_list('id',flat=True)
[you can do that with type(ids)
]. It might be that ids
is an object whose representation is the same as a list, but that it's not a list.
(在我的系统中,您的代码在这种情况下可以工作)。如果它也适用于您,那么当通过p.values_list('id',flat=True)分配id时,研究一下id是什么类型的对象(id)]。可能是id是一个对象,其表示形式与列表相同,但它不是列表。
In this case, you could try typecasting: ids = list(p.values_list('id',flat=True))
before passing it to the json function but there is no guarantee that it will work (it depends if the returned value of p.values_list
is iterable or not.
在本例中,您可以在将其传递给json函数之前尝试typecasting: ids = list(p.values_list('id',flat=True),但不能保证它会工作(这取决于p的返回值)。values_list是否可迭代。
HTH in at least tracking down the issue!
至少追踪这个问题!
#3
3
I encountered the same error and after a lot of confusion, finally the solution that worked for me is as follows in a single answer for the question asked by @afshin:
我遇到了同样的错误,在经历了很多困惑之后,最终对我起作用的解决方案是@afshin所问问题的单一答案:
ids = p.values_list('id',flat=True)
ids_list = list(ids)
import json
json.dumps(ids_list)
#4
1
The problem here is that values_list()
, which is a django function (you probably should clarify this in your question) does not return a list.
这里的问题是values_list(),它是一个django函数(您可能应该在问题中对此进行澄清),它不返回一个列表。
>>> x = UserProfile.objects.values_list('employee_id', flat=True)
>>> type(x)
>>> <class 'django.db.models.query.ValuesListQuerySet'>
simplejson.dumps doesn't know how to serialize this object. So like @mac suggested, you need to typecast this return value to a list.
simplejson。转储不知道如何序列化这个对象。如@mac所建议的,您需要将此返回值输入到列表中。