[英]python: turn nested dict into JSON-like format
I have a nested dict in the form of: 我有以下形式的嵌套字典:
dict = {
"BLOCK_NAME": {
"SUB_BLOCK_NAME1": {
"ENTRY_NUMBER1": {
"FIELD_NAME" : "VALUE"
"FIELD_NAME2" : "VALUE2"
"FIELD_NAME3" : "VALUE3"
}
}
}
} }
I want to display it in an HTML page using a jquery tree table plugin (w2ui). 我想使用jquery树表插件(w2ui)在HTML页面中显示它。 The plugin initializes from data in the following format: 插件将从数据初始化,格式如下:
{ recid: 1, key_1: 'John', key_2: 'doe', w2ui: { children: [] }},
{ recid: 2, key_1: 'Stuart', key_2: 'Motzart',
w2ui: {
children: [
{ recid: 21, key_1: 'Stuart', key_2: 'Motzart',w2ui: { children: [] } },
{ recid: 22, key_1: 'Jin', key_2: 'Franson',
w2ui: {
children: [
and so on... I'm using Jinja2 as a template engine and I'm thinking what's the best way to accomplish this task. 依此类推...我将Jinja2用作模板引擎,并且正在考虑完成此任务的最佳方法。
The options I can think of are: 我能想到的选择是:
Write a python function that transforms dict into a long string that matches that plugin's format and pass it to Jinja. 编写一个python函数,将dict转换为与该插件的格式匹配的长字符串,然后将其传递给Jinja。
Put all the logic inside the template while I iterate over it and create the JS formatting. 在遍历模板并创建JS格式时,将所有逻辑放入模板中。
Save the dict as JSON and process it in JS (less preferable, my JS is weak) 将字典另存为JSON并在JS中处理(不太可取,我的JS较弱)
What do you think? 你怎么看?
EDIT: following @mpf82 answer, I've tried the following: 编辑:以下@ mpf82答案,我尝试了以下操作:
HTML: HTML:
<script type="text/javascript">
$(function () {
$('#grid').w2grid({
name: 'grid',
url : 'get_json',
show: {
toolbar: true,
},
multiSearch: false,
searches: [
{ field: 'lname', caption: 'Last Name', type: 'text' },
{ field: 'fname', caption: 'First Name', type: 'text' },
{ field: 'email', caption: 'Email', type: 'text' },
{ field: 'sdate', caption: 'Start Date', type: 'date' }
],
columns: [
{ field: 'lname', caption: 'Last Name', size: '30%' },
{ field: 'fname', caption: 'First Name', size: '30%' },
{ field: 'email', caption: 'Email', size: '40%' },
{ field: 'sdate', caption: 'Start Date', size: '90px' }
]
});
w2utils.settings['dataType'] = 'JSON'
});
</script>
Cherrypy: Cherrypy:
@cherrypy.expose
@cherrypy.tools.json_in()
@cherrypy.tools.json_out()
def get_json(self):
try:
# optionally get the w2ui request
requested_data = cherrypy.request.json
except:
pass
# build your w2ui data dict
my_data = { recid: 1, fname: 'John', lname: 'doe', email: 'jdoe@gmail.com', sdate: '4/3/2012', w2ui: { children: [] }}
# return dict, no further conversion neccessary
return my_data
I get error 415 from Cherrypy: unsupported media type Expected an entity of content type application/json, text/javascript 我从Cherrypy收到错误415:不支持的媒体类型期望内容类型为application / json,text / javascript的实体
No need to pass your data through jinja or create a long string. 无需通过jinja传递数据或创建长字符串。
Instead, use the w2ui grid's url property, set w2utils to use JSON ( w2utils.settings.dataType = 'JSON';
) and if you're using cherrypy, all you need to do is use the JSON decorators on your URL: 相反,请使用w2ui网格的url属性,将w2utils设置为使用JSON( w2utils.settings.dataType = 'JSON';
),如果您使用的是Cherrypy,您要做的就是在URL上使用JSON装饰器:
@cherrypy.expose
@cherrypy.tools.json_in()
@cherrypy.tools.json_out()
def my_url():
try:
# optionally get the w2ui request
requested_data = cherrypy.request.json
except:
pass
# build your w2ui data dict
my_data = { recid: 1, key_1: 'John', key_2: 'doe', w2ui: { children: [] }}
# return dict, no further conversion neccessary
return my_data
You can simply dump your dict into JSON-like string: 您可以简单地将字典转储为类似JSON的字符串:
import json
# You may need some converting: dict = convert_to_jq_format(dict)
result = json.dumps(dict)
And pass result to Jinja's template. 并将结果传递给Jinja的模板。
Of course, if your dict doesn't match plugin's format, you should convert it first. 当然,如果您的字典与插件的格式不匹配,则应首先将其转换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.