方法如下:

public static string GetJson(object o, Type type) {
    DataContractJsonSerializer jsoner = new DataContractJsonSerializer(type);
    var ms = new MemoryStream();
    jsoner.WriteObject(ms, o);
    byte[] jsonBytes = new byte[ms.Length];
    ms.Position = 0;
    ms.Read(jsonBytes, 0, (int)ms.Length);
    return Encoding.UTF8.GetString(jsonBytes); 
}

在引用System.Runtime.Serialization.Json命名空间时,不但要引用System.Runtime.Serialization(3.0),还要引用System.ServiceModel.Web(3.5)才行。

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-11-04
  • 2022-12-23
  • 2022-12-23
  • 2021-07-24
  • 2021-08-12
  • 2021-11-20
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-11-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-31
相关资源
相似解决方案