我正在使用 Django-Smuggler 将 JSON 数据上传到我的数据库。
当我加载 JSON 文件时,出现错误:No JSON object could be decoded
我使用在线 JSON 验证器来确保数据有效并且确实有效。我通过数据转储获得了这些数据。
有谁知道为什么会这样?
这是引发异常的地方(加星标):
try:
for format, stream in data:
objects = serializers.deserialize(format, stream)
for obj in objects:
model = obj.object.__class__
if router.allow_syncdb(using, model):
models.add(model)
counter += 1
obj.save(using=using)
if counter > 0:
sequence_sql = connection.ops.sequence_reset_sql(style, models)
if sequence_sql:
for line in sequence_sql:
cursor.execute(line)
**except Exception, e:**
transaction.rollback(using=using)
transaction.leave_transaction_management(using=using)
raise e
这里也是我的 JSON 文件的一般格式:
[
{
"pk": 1,
"model": "auth.message",
"fields": {
"message": "Successfully uploaded a new avatar.",
"user": 1
}
},
{
"pk": 2,
"model": "auth.message",
"fields": {
"message": "You have saved model 'mymodel'",
"user": 1
}
},
{
"pk": 3,
"model": "auth.message",
"fields": {
"message": "You have saved model 'sdfsd'",
"user": 1
}
},
{
"pk": 4,
"model": "auth.message",
"fields": {
"message": "Successfully uploaded a new avatar.",
"user": 1
}
},
{
"pk": 5,
"model": "auth.message",
"fields": {
"message": "Successfully updated your avatar.",
"user": 1
}
}
]