问题
What is the best way to deal with JSON in IronPython 2.0.1. The native Python "standard library" json looks to be not implemented yet.
If I wanted to use the Newtonsoft Json.NET library how do I do this? I could add the assembly to the GAC, but what are my other choices?
回答1:
This link provides an overview of the ways to add refernces to .Net dlls with IronPython: Haibo Luo's weblog : IronPython: clr.AddReference
So, for example, if you'd likle to avoid placing the Json.NET library in the GAC you can use
import clr
clr.AddReferenceToFile("jsonnet.dll")
or
clr.AddReferenceToFileAndPath("C:\\libraries\\jsonnet.dll")
回答2:
#list with data
data=[]
item={}
item["name"]="joe's pizza"
item["tel"] = "343-4333"
data.append(item)
#returns: [{'tel': '343-4333', 'name': "joe's pizza"}]
#but not valid JSON
print str(data)
#returns [{"tel":"343-4333","name":"joe\u0027s pizza"}]
import clr
clr.AddReference('System.Web.Extensions')
from System.Web.Script.Serialization import JavaScriptSerializer #since .net 3.5?
json=JavaScriptSerializer().Serialize(data)
print str(json)
来源:https://stackoverflow.com/questions/799215/ironpython-json-choices