.NET序列化反序列化对象并写入JSON文件(读取JSON并反射其属性与值)

不使用NEWTONSOFT的JSON库,使用.net framework 4.6,序列化对象并存入JSON文件;然后读取JSON文件内容并反序列化对象;使用反射方法读取其所有属性与属性值,直接上代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using System;
using System.IO;
using System.Text;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Reflection;

namespace ConsoleApp1
{
[DataContract]
class MyConfig
{
[DataMember]
public string IP
{
get; set;
}
[DataMember]
public int Port
{
get; set;
}
}

class Program
{
const string CONFIG_JSON = ".config";

static void Main(string[] args)
{

MyConfig config = new MyConfig();

Console.WriteLine("IP:");
config.IP = Console.ReadLine();
Console.WriteLine("Port:");
config.Port = int.Parse(Console.ReadLine());

string json = JSONSerializer<MyConfig>.Serialize(config);
File.WriteAllText(CONFIG_JSON, json);
Console.WriteLine("写入JSON文件.config\r\n开始读取文件内容");

json = File.ReadAllText(CONFIG_JSON);
Console.WriteLine("读取JSON文件.config\r\n" + json);
config = JSONSerializer<MyConfig>.DeSerialize(json);

Type t = config.GetType();
PropertyInfo[] pArray = t.GetProperties();
for (int i = 0; i < pArray.Length; i++)
{
Console.WriteLine("属性" + i + ": " + pArray[i].Name + " " + pArray[i].GetValue(config));
}

Console.Read();

}
}

public static class JSONSerializer<TType> where TType : class
{
/// <summary>
/// Serializes an object to JSON
/// </summary>
public static string Serialize(TType instance)
{
var serializer = new DataContractJsonSerializer(typeof(TType));
using (var stream = new MemoryStream())
{
serializer.WriteObject(stream, instance);
return Encoding.Default.GetString(stream.ToArray());
}
}

/// <summary>
/// DeSerializes an object from JSON
/// </summary>
public static TType DeSerialize(string json)
{
using (var stream = new MemoryStream(Encoding.Default.GetBytes(json)))
{
var serializer = new DataContractJsonSerializer(typeof(TType));
return serializer.ReadObject(stream) as TType;
}
}
}
}

执行后会在当前路径生成.config文件。