Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django和解析json的问题_Python_Django_Json_Post_Request - Fatal编程技术网

Python Django和解析json的问题

Python Django和解析json的问题,python,django,json,post,request,Python,Django,Json,Post,Request,我正在尝试获取发布的json,并将该对象导入django数据库。 Json看起来像: { "pooledSets" : [ { "name" : "Pooled Set 1", "madeBy" : "John Doe", "tags" : ['thing1','thing2' ], "sets" : [ { "library" : {

我正在尝试获取发布的json,并将该对象导入django数据库。
Json看起来像:

{
"pooledSets" : [
    {
        "name" : "Pooled Set 1",
        "madeBy" : "John Doe",
        "tags" : ['thing1','thing2' ],
        "sets" : [
            {
                "library" : {
                "replicate" : 2,
                "name" : "My library name",
                "referenceGenome" : "hg19-male"
            }
        ]
    },
            {
        "name" : "Pooled Set 1",
        "madeBy" : "John Doe",
        "tags" : ['thing1','thing2' ],
        "sets" : [
            {
                "library" : {
                "replicate" : 2,
                "name" : "My library name",
                "referenceGenome" : "hg19-male"
            }
        ]
    }
]
}
import json
from django.http import HttpResponse
from piston.handler import BaseHandler
from lab.pooledsets.models import PooledSet
from django.http import QueryDict

class PooledSetJsonHandler(BaseHandler):
    allowed_methods = ('POST')

    def create(self, request):          
        for myPooledSet in request.POST.getlist('pooledSets'):
            myDict = QueryDict(myPooledSet, 'json')
            print myDict
            myName = myDict.__getitem__('name')
            print myName
我的代码如下所示:

{
"pooledSets" : [
    {
        "name" : "Pooled Set 1",
        "madeBy" : "John Doe",
        "tags" : ['thing1','thing2' ],
        "sets" : [
            {
                "library" : {
                "replicate" : 2,
                "name" : "My library name",
                "referenceGenome" : "hg19-male"
            }
        ]
    },
            {
        "name" : "Pooled Set 1",
        "madeBy" : "John Doe",
        "tags" : ['thing1','thing2' ],
        "sets" : [
            {
                "library" : {
                "replicate" : 2,
                "name" : "My library name",
                "referenceGenome" : "hg19-male"
            }
        ]
    }
]
}
import json
from django.http import HttpResponse
from piston.handler import BaseHandler
from lab.pooledsets.models import PooledSet
from django.http import QueryDict

class PooledSetJsonHandler(BaseHandler):
    allowed_methods = ('POST')

    def create(self, request):          
        for myPooledSet in request.POST.getlist('pooledSets'):
            myDict = QueryDict(myPooledSet, 'json')
            print myDict
            myName = myDict.__getitem__('name')
            print myName
跑步时,myDict表现良好:

<QueryDict: {u"{u'name': u'Pooled Set 1', u'tags': [u\'thing1\', u\'thing2\'], u'sets': etc etc etc
发生了什么,我该如何提取键值对?

您的dict结果不太好。看看它:

{u"{u'name': u'Pooled Set 1', u'tags': [u\'thing1\', u\'thing2\'], u'sets': …}
您有一个dict,其键是表示dict的JSON字符串(其值尚未显示)<代码>“名称”不是该命令中的键<代码>“{u'name':u'poolled Set 1',u'tags':[u'thing1\',u'thing2\',u'Set':…}”

如果不知道原始数据是什么,甚至不知道解析后的
myDict
的其余部分是什么样子(您只向我们展示了dict中第一个键的一部分),就很难确定您到底应该写什么,而不是实际写了什么

你当然可以这样做:

for key, value in myDict.items():
    actual_dict = json.loads(key) # does the value mean anything?
    print actual_dict['name']

…但是错误地解析数据,然后在事后试图修复它们几乎总是一个坏主意。

您考虑过使用fixture吗? 假设您已经有一个django模型格式正确的json文件, 你可以跑了

python manage.py loaddata fixture_name.json
它将直接更新您的模型

参考:

作为旁注,您为什么要编写
myDict.\uuu getitem\uuuu('name')
而不仅仅是
myDict['name']
myDict.get('name')
?与此同时,虽然您已经向我们展示了足够的数据来诊断问题,但您还没有向我们展示足够的数据来解决问题。您的输出仅显示
pooledSets
中第一个dict中第一个键的一部分。数据结构显然不应该是由代表dict的JSON字符串键控的dict序列……但它应该是什么却不太明显。