我在使用mongoengine将JSON请求保存到MongoDB中的DynamicDocument时遇到问题
这是我的文件:
class documentSource(DynamicEmbeddedDocument):
name = StringField()
class documentParent(Document):
name = StringField(max_length=120)
source = ListField(EmbeddedDocumentField('documentSource'))
这是我的request POST对象:
{
"name": "Test",
"source": [{
"name": "my first source"
"metadata": {
"name": "testing",
"products": [
{"name":"my product", "price":123}
]
}
},{
"name": "my second source"
"metadata": {
"name": "Test",
"profile": "foo"
}
}
]
}
以下是我的烧瓶贴法:
def post(self):
myObj = documentParent(
name=self.data['name'],
description=self.data['description'],
)
sourceList = []
for i in self.data['source']:
content = documentSource(**i)
sourceList.append(content)
myObj.source = sourceList
myObj.save()
但问题是:
如果我发送的JSON不起作用:
{
"name": "Test",
"source": [{
"name": "my first source"
"metadata": {
"name": "testing",
"products": [
{"name":"my product", "price":123}
]
}
},{
"name": "my second source"
"metadata": {
"name": "Test",
"profile": "foo",
"foo" : {
"foo1": "var1"
}
}
}
]
}
但是这个物体起作用了:
{
"name": "Test",
"source": [{
"name": "my first source"
"metadata": {
"name": "testing",
"products": [
"my product"
]
}
},{
"name": "my second source"
"metadata": {
"name": "Test",
"profile": "foo"
}
}
]
}
列表列表也有同样的问题:
"image":
{"available_sizes":
[[[150,
19],
"assets/images/150x150.png"],
[[250,
31],
"assets/images/250x250.png"],
[[450,
57],
"assets/images/450x450.png"]]
我认为对于复杂的json结构,mongoengine的解析器不起作用。我不知道如何解决这个问题,因为我无法控制源代码信息,最重要的是从源代码(例如:网站爬虫)获取一个JSON对象并将其保存到我的动态文档中
事先谢谢你的帮助
“我的第一个来源”和“我的第二个来源”后面缺少逗号。您的JSON无效
有效期:
}
用于验证JSON的漂亮工具:
https://jsonlint.com/
相关问题 更多 >
编程相关推荐