字典转Json

pro
((10106L, 'nod01-192.168.137.2'), (10093L, 'Template App FTP Service'), (10094L, 'Template App HTTP Service'), (10095L, 'Template App HTTPS Service'), (10096L, 'Template App IMAP Service'), (10097L, 'Template App LDAP Service'), (10073L, 'Template App MySQL'), (10098L, 'Template App NNTP Service'), (10099L, 'Template App NTP Service'), (10100L, 'Template App POP Service'), (10101L, 'Template App SMTP Service'), (10102L, 'Template App SSH Service'), (10103L, 'Template App Telnet Service'), (10050L, 'Template App Zabbix Agent'), (10048L, 'Template App Zabbix Proxy'), (10047L, 'Template App Zabbix Server'), (10104L, 'Template ICMP Ping'), (10071L, 'Template IPMI Intel SR1530'), (10072L, 'Template IPMI Intel SR1630'), (10082L, 'Template JMX Generic'), (10083L, 'Template JMX Tomcat'), (10076L, 'Template OS AIX'), (10075L, 'Template OS FreeBSD'), (10077L, 'Template OS HP-UX'), (10001L, 'Template OS Linux'), (10079L, 'Template OS Mac OS X'), (10074L, 'Template OS OpenBSD'), (10078L, 'Template OS Solaris'), (10081L, 'Template OS Windows'), (10066L, 'Template SNMP Device'), (10068L, 'Template SNMP Disks'), (10065L, 'Template SNMP Generic'), (10060L, 'Template SNMP Interfaces'), (10069L, 'Template SNMP OS Linux'), (10067L, 'Template SNMP OS Windows'), (10070L, 'Template SNMP Processors'), (10088L, 'Template Virt VMware'), (10089L, 'Template Virt VMware Guest'), (10091L, 'Template Virt VMware Hypervisor'), (10105L, 'testtlcb'), (10084L, 'Zabbix server'), (10092L, '{#HV.NAME}'), (10090L, '{#VM.NAME}'))
<type 'tuple'>
10106
nod01-192.168.137.2
----------
10093
Template App FTP Service
----------
10094
Template App HTTP Service
----------



 cur = conn.cursor()
        a=cur.execute("select hostid,name from hosts")
        info = cur.fetchall()
        d={}
        print info
        print type(info)
        for s in info:
            print s[0]
            print s[1]
            d[s[0]]=s[1]
            print '----------'
        print d
        resp = {'aaa': 100, 'bbb': '200'}

列表转字典:

{10001L: 'Template OS Linux', 10047L: 'Template App Zabbix Server', 10048L: 'Template App Zabbix Proxy', 10050L: 'Template App Zabbix Agent', 10060L: 'Template SNMP Interfaces', 10065L: 'Template SNMP Generic', 10066L: 'Template SNMP Device', 10067L: 'Template SNMP OS Windows', 10068L: 'Template SNMP Disks', 10069L: 'Template SNMP OS Linux', 10070L: 'Template SNMP Processors', 10071L: 'Template IPMI Intel SR1530', 10072L: 'Template IPMI Intel SR1630', 10073L: 'Template App MySQL', 10074L: 'Template OS OpenBSD', 10075L: 'Template OS FreeBSD', 10076L: 'Template OS AIX', 10077L: 'Template OS HP-UX', 10078L: 'Template OS Solaris', 10079L: 'Template OS Mac OS X', 10081L: 'Template OS Windows', 10082L: 'Template JMX Generic', 10083L: 'Template JMX Tomcat', 10084L: 'Zabbix server', 10088L: 'Template Virt VMware', 10089L: 'Template Virt VMware Guest', 10090L: '{#VM.NAME}', 10091L: 'Template Virt VMware Hypervisor', 10092L: '{#HV.NAME}', 10093L: 'Template App FTP Service', 10094L: 'Template App HTTP Service', 10095L: 'Template App HTTPS Service', 10096L: 'Template App IMAP Service', 10097L: 'Template App LDAP Service', 10098L: 'Template App NNTP Service', 10099L: 'Template App NTP Service', 10100L: 'Template App POP Service', 10101L: 'Template App SMTP Service', 10102L: 'Template App SSH Service', 10103L: 'Template App Telnet Service', 10104L: 'Template ICMP Ping', 10105L: 'testtlcb', 10106L: 'nod01-192.168.137.2'}
[12/Dec/2017 11:03:11] "GET /api/getTemp/?env=pro HTTP/1.1" 200 26


 a=cur.execute("select hostid,name from hosts")
        info = cur.fetchall()
        d={}
        print info
        print type(info)
        for s in info:
            print s[0]
            print s[1]
            d[s[0]]=s[1]
            print '----------'
        print d
        resp=d
        #resp = {'aaa': 100, 'bbb': '200'}
        return HttpResponse(json.dumps(resp), content_type="application/json")

原文地址:https://www.cnblogs.com/hzcya1995/p/13349375.html