2

我正在使用 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
}
  }
]
4

2 回答 2

7

您需要删除 JSON 字符串中的最后一个 ',' 字符。

于 2012-06-20T14:27:06.363 回答
4

我对此有点晚了,但您的 JSON 文件可能不是那么有效,即使它通过了 jsonlint。检查它是否使用没有 BOM编码的 UTF-8 。

于 2012-09-01T00:58:49.467 回答