In Fedora 17 64bit while using netifaces and json imports.
I'm trying to get this format in JSON
"net_info" : [ {"nic" : ..., "mac" : ..., "ip" : ...}, {"nic" : ..., "mac" : ..., "ip" : ...}, {"nic" : ..., "mac" : ..., "ip" : ...}, ]
I'm currently using a string and just appending to it, and I get this:
"'net_info': [{'nic':eth0,'mac':6c:f0:49:0f:e1:c2,'ip':192.168.1.116},]"
This may not work due to the quotes at the beginning and the end of each string; is there a better way of accomplishing this? I was thinking of using a List of Dictionaries but ended up trying strings first, not sure what would best in this case.
Here's my code that takes in 3 lists:
def json_serialize(ip=[],mac=[],nic=[]):
jsonDump = "'net_info': ["
for i,item in enumerate(ip):
jsonDump += "{'interface_name':" + nic[i] +",'mac':"
+ mac[i] + ",'ip':" + ip[i] +"},"
jsonDump += "]"
print jsonDump.strip()
#Testing output after its passed in to json.dumps(), it now has quotes at beginning
#and end of string...?
print "\n"
print json.dumps(jsonDump)
{..}around it for starters.