VS无法解析DataContractJsonSerializer

今天把之前项目中写的一段转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

Your email address will not be published. Required fields are marked *