永发信息网

django json数据解析

答案:2  悬赏:0  手机版
解决时间 2021-03-06 03:11
  • 提问者网友:藍了天白赴美
  • 2021-03-05 09:00
{"GZWWWVIRUS": {"endflag": "2", "version": "RB_Virus_20150507_5", "updated_at": "2015-05-08 11:54:21"}, "BJWWWHORNBILL": {"endflag": "2", "version": "RB_Whornbill_20150507_8", "updated_at": "2015-05-08 13:45:41"}, "BJDOOTAVDOOTA": {"endflag": "2", "version": "RB_Vdoota_20150507_8", "updated_at": "2015-05-08 11:35:02"}, "GZTINTERFACEWHORNBILL": {"endflag": "2", "version": "RB_Whornbill_20150507_8", "updated_at": "2015-05-08 12:36:43"}, "GZTINTERFACESNAKE": {"endflag": "2", "version": "RB_Snake_20150507_5", "updated_at": "2015-05-08 12:35:07"}, "GZWWWHORNBILL": {"endflag": "2", "version": "RB_Whornbill_20150507_8", "updated_at": "2015-05-08 13:49:06"}, "GZWWWSNAKE": {"endflag": "2", "version": "RB_Snake_20150507_5", "updated_at": "2015-05-08 11:55:30"}}这样的数据在数据库,我如何获取并且遍历解析。
最佳答案
  • 五星知识达人网友:夜余生
  • 2021-03-05 10:29
python自带的JSON处理就OK

a = '{"GZWWWVIRUS": {"endflag": "2", "version": "RB_Virus_20150507_5", "updated_at": "2015-05-08 11:54:21"}, "BJWWWHORNBILL": {"endflag": "2", "version": "RB_Whornbill_20150507_8", "updated_at": "2015-05-08 13:45:41"}, "BJDOOTAVDOOTA": {"endflag": "2", "version": "RB_Vdoota_20150507_8", "updated_at": "2015-05-08 11:35:02"}, "GZTINTERFACEWHORNBILL": {"endflag": "2", "version": "RB_Whornbill_20150507_8", "updated_at": "2015-05-08 12:36:43"}, "GZTINTERFACESNAKE": {"endflag": "2", "version": "RB_Snake_20150507_5", "updated_at": "2015-05-08 12:35:07"}, "GZWWWHORNBILL": {"endflag": "2", "version": "RB_Whornbill_20150507_8", "updated_at": "2015-05-08 13:49:06"}, "GZWWWSNAKE": {"endflag": "2", "version": "RB_Snake_20150507_5", "updated_at": "2015-05-08 11:55:30"}}'

b = JSONDecoder().decode(a)

print b['GZWWWVIRUS']
全部回答
  • 1楼网友:佘樂
  • 2021-03-05 10:36
因为客户端传递过来的数据是json数据,可以看成一个json对象。不是传过来的post表单,所以你用request.post.get('id', '')这种肯定取不到值的。 def datasave(request):     dict = {}     info = 'data log save success'     try:         if request.method == 'post':             req = simplejson.loads(request.raw_post_data)             username = req['username']             password = req['password']             datas = req['datas']             game_id1 = datas[0]['game_id']    except:         import sys         info = "%s || %s" % (sys.exc_info()[0], sys.exc_info()[1])     dict['message']=info     dict['create_at']=str(ctime())     json=simplejson.dumps(dict)     return httpresponse(json)    request.raw_post_data表示的是从客户端发送过来的原始数据,为了纯字符串, 通过simplejson的loads方法将其转换为字典数据类型req.     上面的代码也演示了如何以json格式作为响应值, 而非html, 即通过simplejson的dumps方法, 将字典数据dict序列化为字符串形式,将通过httpresponse返回.
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯