0

我想在django视图中构造这样的json:

{
    "Skills": [
        {
            "Name": "Java",
            "Value": "Java"
        },
        {
            "Name": "J2ee",
            "Value": "J2ee"
        },
        {
            "Name": "Python",
            "Value": "Python"
        },
        {
            "Name": "Django",
            "Value": "Django"
        }
    ]
}

python simplejson 不会像上面那样创建它,请提供任何建议。基本上我正在发送对 select2 标签的响应。我正在从 Jquery ajax 调用视图方法..

def populateSkills(request):
    print "Inside Populate Skills"
    preload_data = '{"Skills":[{"Name":"Java","Value":"Java"},{"Name":"J2ee","Value":"J2ee"},{"Name":"Python","Value":"Python"},{"Name":"Django","Value":"Django"}]}'
    return HttpResponse(simplejson.dumps(preload_data), content_type="application/json")

我对 preload_data 进行了硬编码,因为我无法构建它。那么我该如何构建它呢?有 JSONObject 来做吗?

4

1 回答 1

2

使用simplejson

import simplejson
simplejson.dumps({'Skills': [{'Name': 'Java', 'Value': 'Java'}, {'Name': 'J2ee', 'Value': 'J2ee'}, {'Name': 'Python', 'Value': 'Python'}, {'Name': 'Django', 'Value': 'Django'}] })
于 2013-02-22T09:46:29.847 回答