1.先创建一个File对象,然后引用using System.Runtime.Serialization.再分别在类和成员上添加DataContract和DataMember.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
namespace JSONDemo
{
[DataContract]
public class File
{
public Guid Id { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public int Size { get; set; }
}
}
2.调用的代码如下,实例化对象,然后序列化.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using GongHuiNewtonsoft.Json;
using GongHuiNewtonsoft.Json.Serialization;
using GongHuiNewtonsoft.Json.Converters;
namespace JSONDemo
{
class Program
{
static void Main(string[] args)
{
File file = new File
{
I