我正在调用有关我的基础设施的一些信息,并尝试构建一个字典,我可以将其转换为 json 作为对象并将其传递给 javascript 进行显示。
我尝试过使用 json.dumps 和 simplejson.dumps 到字典中。
我假设这是因为字典中嵌套了字典。
这是数据的结构
当数据通过 django 传递到网页时,python 打印出数据。
{'SecurityGroup:eiddo-slave-elb': {'rules': {'ingress': {'to_port': '0000',
'ingress_grants': "{'sg-000000': {'cidr': 'None', 'group_id': 'sg-00000',
'grant_name': 'None'}}", 'from_port': '7001', 'protocol': 'tcp', 'groups': ''},
'egress': {'to_port': '0001', 'egress_grants': "{'sg-000000': {'cidr': 'None',
'group_id': 'sg-00000', 'grant_name': 'None'}}", 'from_port': '0000', 'protocol': 'tcp', 'groups': ''}}, 'id': 'SecurityGroup:eiddo-slave-elb', 'tags': '{}'}}
网络控制台输出数据。语法错误来自 jquery,因为它无法解析数据来完成循环。正如您所看到的,传递的数据中有很多特殊字符。我什至尝试将 dict 的字符串编码为 UTF-8
Error: Syntax error, unrecognized expression: {'SecurityGroup:eiddo-slave-elb': {'rules': {'ingress': {'to_port':
'7001', 'ingress_grants': "{'sg-000000000':
{'cidr': 'None', 'group_id': 'sg-54b09d31',
'grant_name': 'None'}}", 'from_port':
'7001', 'protocol': 'tcp', 'groups': ''},
'egress': {'to_port': '0000', 'egress_grants':
"{'sg-00000000': {'cidr': 'None',
'group_id': 'sg-00000000', 'grant_name':
'None'}}", 'from_port': '7001', 'protocol':
'tcp', 'groups': ''}}, 'id':
'SecurityGroup:eiddo-slave-elb', 'tags': '{}'},
'SecurityGroup:asgard': {'rules': {'ingress':
{'to_port': 'None', '
JS var security_groups = "{{security_groups}}";
$(security_groups).each(function( index ) {
console.log(security_groups[index]);
});
Python 代码
def getSG():
conn = boto.ec2.connect_to_region('us-west-2',aws_access_key_id='', aws_secret_access_key='')
rs = conn.get_all_security_groups()
rule_list = {}
ig_grants = {}
eg_grants = {}
sg_dict = {}
flag = True
for item in rs:
rules = item.rules
for rule in item.rules:
for grant in rule.grants:
ig_grants[str(grant)] = {'cidr': str(grant.cidr_ip).strip(),
'group_id': str(grant.group_id).strip(),
'grant_name': str(grant.name).strip()}
rule_list['ingress']= {'from_port': str(rule.from_port).strip(),
'to_port': str(rule.to_port).strip(),
'groups': str(rule.groups).strip(),
'protocol': str(rule.ip_protocol).strip(),
'ingress_grants': str(ig_grants).strip()
}
ig_grants = {}
for rule in item.rules:
for grant in rule.grants:
eg_grants[str(grant)] = {'cidr': str(grant.cidr_ip).strip(),
'group_id': str(grant.group_id).strip(),
'grant_name': str(grant.name).strip()}
rule_list['egress']= {'from_port': str(rule.from_port).strip(),
'to_port': str(rule.to_port).strip(),
'groups': str(rule.groups).strip(),
'protocol': str(rule.ip_protocol).strip(),
'egress_grants': str(eg_grants).strip()
}
eg_grants = {}
sg_dict[str(item)] = {'id':str(item).strip(), 'rules':rule_list, 'tags':str(item.tags).strip()}
rule_list = {}
return sg_dict
def index(request):
conn = boto.ec2.connect_to_region('us-west-2',aws_access_key_id='', aws_secret_access_key='')
#instances = get_ec2_instances(conn)
rs = conn.get_all_security_groups()
security_groups = getSG()
print security_groups
template = loader.get_template('index.html')
context = RequestContext(request, {'security_groups': security_groups,})
return HttpResponse(template.render(context))
我这里的目标是将其作为json字符串传递给js并将其转换为json对象。
我知道我没有在 js 中包含 JSON.parse,因为我想让字符串在没有特殊字符的情况下看起来正确。因为当我这样做时
JSON.parse(security_groups);
我收到以下错误:
语法错误:JSON.parse:JSON 数据第 1 行第 2 列处的预期属性名称或“}”
最佳答案
模板中的
{{ security_groups }}
输出 Python 字典的 HTML 转义表示形式。这不是 Javascript 可以正确解析的东西。
相反,请使用 JSON(又名 JavaScript 对象表示法),JS 会知道如何加载。
<小时/>为此,
在您的 View 代码中,更改
context = RequestContext(request, {'security_groups': security_groups,})
至:
context = RequestContext(request, {'security_groups': json.dumps(security_groups),})
(您需要在文件顶部添加 import json
)
并在 JS 代码中更改:
var security_groups = "{{security_groups}}";
至:
var security_groups = JSON.parse("{{ security_groups|escapejs }}");
或者简单地(但不推荐):
var security_groups = {{ security_groups }};
关于javascript - 特殊字符显示在由 AWS 构建的 Python Dict 中,并通过 django View 传递给 js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31124755/