删除\“字符在返回json时 [英] Remove \" characters while returning json

查看:101
本文介绍了删除\“字符在返回json时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过Python / Django将数据作为JSON返回。



我使用以下代码:

  jsonObject = serializers.serialize ('json',object,fields =('value','record_time'))
return HttpResponse(json.dumps(jsonObject),mimetype =application / json)

问题是我得到如下响应:

 {{\pk\:null,\model\:\model.table\,\fields\:{\record_time \:\2009-11-18T22:45:44Z\,\value\:0.6533125480476399}},{\pk\:null,\model\ :\model.table\,\fields\:{\record_time\:\2009-11-19T15:02:15Z \,\value\ :0.4386133754377899}}]

我应该以下列格式获取:

 [
{
pk:null,
model:model.table ,
fields:
{
record_time: 2009-11-18T22:45:44Z,
value:0.6533125480476399
}
},
{
pk:null,
model:model.table,
fields:
{
record_time:2009-11-19T15:02:15Z,
value :0.4386133754377899
}
}
]

在这里出错?

解决方案

您正在将对象序列化为 jsonObject 但是您返回原始的对象



更新:



只需修复即可更改你的代码如下:

  jsonObject = serializers.serialize('json',object,fields =('value' record_time'))
return HttpResponse(jsonObject,mimetype =application / json)

更改是第二行中的 jsonObject


I'm returning data as a JSON via Python/Django.

I used the below code:

jsonObject = serializers.serialize('json', object, fields=('value', 'record_time'))
return HttpResponse(json.dumps(jsonObject), mimetype="application/json")

The problem is I'm getting the response as below:

"[{\"pk\": null, \"model\": \"model.table\", \"fields\": {\"record_time\": \"2009-11-18T22:45:44Z\", \"value\": 0.6533125480476399}}, {\"pk\": null, \"model\": \"model.table\", \"fields\": {\"record_time\": \"2009-11-19T15:02:15Z\", \"value\": 0.4386133754377899}}]"

where as I should be getting it in below format:

"[
  {
   "pk": null, 
   "model": "model.table", 
   "fields": 
      {
        "record_time": "2009-11-18T22:45:44Z", 
        "value": 0.6533125480476399
      }
  }, 
  {
    "pk": null, 
    "model": "model.table", 
    "fields": 
      {
        "record_time": "2009-11-19T15:02:15Z", 
        "value": 0.4386133754377899
      }
  }
]"

Where am I going wrong here?

解决方案

You are serializing the object into jsonObject, but you return the original object. Just fix that and everything should work as expected.

Update:

Just change your code like this:

jsonObject = serializers.serialize('json', object, fields=('value', 'record_time'))
return HttpResponse(jsonObject, mimetype="application/json")

The change is the jsonObject in the second line.

这篇关于删除\“字符在返回json时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆