今天把之前项目中写的一段转JSON的代码复制到Windows Phone的项目的代码里,结果发现Visual Studio无法解析DataContractJsonSerializer类,
public static string JsonSerialize<T>(T obj)
{
string retVal = null;
var serializer = new DataContractJsonSerializer(obj.GetType());
using (MemoryStream ms = new MemoryStream())
{
serializer.WriteObject(ms, obj);
byte[] bytes = ms.ToArray();
retVal = Encoding.UTF8.GetString(bytes, 0, bytes.Length);
}
return retVal;
}
解决方法,在referrence里加入下面2个dll
- System.ServiceModel.dll
- System.ServiceModel.Web.dll
Leave a Reply
You must be logged in to post a comment.