从我的JSON数据中删除u'infront [英] Remove u' infront of my JSON data

查看:98
本文介绍了从我的JSON数据中删除u'infront的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python的新手,看来我所有的JSON数据都与u'前缀组合在一起,

I'm new to python and it seems that all my JSON data is combined with u' prefix as such:

{u'number': u'12345666', u"items"...}

我不需要此数据(unicode或其他),因为我想将 string 打印到Javascript变量中:

I don't need this data (unicode or whatever) as I want to print the string into a Javascript variable:

var obj = data; // data is the object above.

我的python看起来像这样;

My python looks something like this;

index.html:
var obj = ${data};

我正在使用moko框架进行模板制作.

I'm using the moko framework for templating.

// getitems() return {'number':'12312...}
context = {'data': getitems(self)}
self.render_response('index.html',**context)

已处理的javascript输出数据如下:

The processed javascript output data look like this:

var obj = {u'number': u'12345666', u"items"...} 

这是我的问题.

推荐答案

问题是您正在将字典转换为字符串(可能是Mako为您执行了str(...)).但是您应该 jsonify 它,即

The problem is that you are converting a dictionary to a string (probably Mako does str(...) for you). But you should jsonify it, i.e.

import json
context = { 'data': json.dumps(getitems(self)) }

这篇关于从我的JSON数据中删除u'infront的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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