在HttpResponse中传递两个JSON数据

时间:2014-10-30 02:22:07

标签: json django

如何正确返回两个JSON数据?我在下面尝试了这段代码,但它返回了一个SyntaxError: Unexpected token {的错误。我试着在控制台中查看,这是示例结果:

{"landarea": ["93.405145"], "brgy": ["BAAN KM 3"], "taxdec": ["08-11-0001-02746"]}{"mydescription": ["Built-up Areas"]}

显然,{就在那里找到了。

这是我的views.py:

def pins_info(request):
    if request.method == "GET":
        getpin = request.GET.get('pin', None)
        keyvals = OrderedDict([
            ('taxdec', 'landproperty__ctaxdec'),
            ('brgy', 'ssectionid__sbrgyid__cbrgyname'),
            ('landarea', 'narea')
        ])
        keyvals1 = OrderedDict([
            ('mydescription', 'description')
        ])

        m = ButuanMaps.objects.filter(clandpin=getpin).values_list(*keyvals.values())
        reference = ButuanMaps.objects.get(clandpin=getpin).geom
        within = ErosionMap.objects.filter(geom__contains=reference).values_list(*keyvals1.values())
        #result_list = list(chain(m, within))
        result = dict(zip(keyvals, zip(*m)))
        result1 = dict(zip(keyvals1, zip(*within)))
        data = json.dumps(result, cls=DjangoJSONEncoder)
        data1 = json.dumps(result1, cls=DjangoJSONEncoder)
        return HttpResponse({data,data1}, content_type='application/json')

1 个答案:

答案 0 :(得分:2)

为什么不创建一个封装示例中的两个对象的json对象:

var result = 
{
     "land" : {
       "landarea": ["93.405145"], 
       "brgy": ["BAAN KM 3"], 
       "taxdec": ["08-11-0001-02746"]
     },
     "description" : {
       "mydescription": ["Built-up Areas"]
     }
}