IronPython - JSON choices

江枫思渺然 提交于 2019-12-07 10:55:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!