gpt4 book ai didi

javascript - 解析通过ajax返回的多个json列表

转载 作者:行者123 更新时间:2023-11-28 00:46:06 25 4
gpt4 key购买 nike

我通过ajax以json形式返回了两个对象。现在我无法访问这两个列表中的值。早些时候我有一个列表,我能够轻松解析它。

data=serialize("json", vm_obj)
data2=serialize("json", user_networks_list)

data_dict["vm"] = vm_obj
data_dict["nt"] = user_networks_list

json = simplejson.dumps({'vm': data, 'nt': data2})
return HttpResponse(json, mimetype='application/javascript')

所以,我必须列出名称 nt 和 vm。我返回的 json 看起来像。

{"nt": "[{"pk": 1, "model": "network.defaultnetwork", "fields": {"endip": "100.16.10.255", "subnet": "100.16.10.0/24", "startip": "100.16.10.25", "port_group": "RoyalTech-803-pg", "is_default": 1, "eth_interface": "eth1", "dnsname": "royaltech.net", "user": 803, "dns": "172.16.10.1", "networkname": null, "gateway": "100.16.10.1"}}, {"pk": 241, "model": "network.defaultnetwork", "fields": {"endip": "100.168.5.254", "subnet": "100.168.5.0/24", "startip": "100.168.5.25", "port_group": "RoyalTech-803-pg1", "is_default": 0, "eth_interface": "eth2", "dnsname": "latest.local", "user": 803, "dns": "8.8.8.8", "networkname": "maxcheck", "gateway": "100.168.5.1"}}, {"pk": 2, "model": "network.defaultnetwork", "fields": {"endip": "10.250.0.254", "subnet": "10.250.0.0/24", "startip": "10.250.0.25", "port_group": null, "is_default": 0, "eth_interface": "eth3", "dnsname": "testing network", "user": 803, "dns": "100.0.0.0", "networkname": "Testing network", "gateway": "100.111.0.1"}}]", "vm": "[{"pk": 700, "model": "virtualmachine.virtualmachines", "fields": {"guest_full_name": "Windows_server_2008_R2_x86_64", "remoting": true, "vm_path_name": "/v/Roya-hd28_1.img", "user_type": null}}]"}

之前我有一个列表,我能够使用以下 JavaScript 循环访问数据。

for (var i = 0; i < data.length; i++)
{
visitors = data[i].pk;
object = data[i]['fields'].name;
}

你知道 JavaScript 现在会是什么样子吗?

最佳答案

您正在对子词典中的 JSON 进行双重编码。您应该首先序列化为 Python 字典,然后仅在最后将所有内容转储为 JSON:

data=serialize("python", vm_obj)
data2=serialize("python", user_networks_list)

data_dict["vm"] = vm_obj
data_dict["nt"] = user_networks_list

json = simplejson.dumps({'vm': data, 'nt': data2})

关于javascript - 解析通过ajax返回的多个json列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27360778/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com