1 实体类的 Json 序列化和反序列化
我们以如下的 Person 类举例,其中包含了常用的数据类型:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
public class Person { public int ID { get ; set ; } public string Name { get ; set ; } public DateTime Birthday { get ; set ; } public bool IsVIP { get ; set ; } public float Account { get ; set ; } public string [] Favorites { get ; set ; } public string Remark { get ; set ; } } |
创建一个 Person
实例:
1
2
3
4
5
6
7
8
9
|
Person person = new Person { ID = 1, Name = "张三" , Birthday = DateTime.Parse( "2000-01-02" ), IsVIP = true , Account = 12.34f, Favorites = new string [] { "吃饭" , "睡觉" } }; |
1.1 Json 序列化
返回不缩进的 Json 字符串
1
2
3
|
JsonConvert.SerializeObject(person); { "ID" :1, "Name" : "张三" , "Birthday" : "2000-01-02T00:00:00" , "IsVIP" : true , "Account" :12.34, "Favorites" :[ "吃饭" , "睡觉" ], "Remark" : null } |
返回缩进的 Json 字符串
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
JsonConvert.SerializeObject(person, Formatting.Indented); { "ID" : 1, "Name" : "张三" , "Birthday" : "2000-01-02T00:00:00" , "IsVIP" : true , "Account" : 12.34, "Favorites" : [ "吃饭" , "睡觉" ], "Remark" : null } |
1.2 将不缩进的 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
|
private string JsonIndentation( string str) { //string str = JsonConvert.SerializeObject(entity); JsonSerializer serializer = new JsonSerializer(); TextReader tr = new StringReader(str); JsonTextReader jtr = new JsonTextReader(tr); object obj = serializer.Deserialize(jtr); if (obj != null ) { StringWriter textWriter = new StringWriter(); JsonTextWriter jsonWriter = new JsonTextWriter(textWriter) { Formatting = Formatting.Indented, Indentation = 4, IndentChar = ' ' }; serializer.Serialize(jsonWriter, obj); return textWriter.ToString(); } else { return str; } } |
或者:
1
2
3
4
5
|
private string JsonIndentation( string json) { JObject obj = JObject.Parse(json); return obj.ToString(); } |
1.3 其他设置
1
2
3
4
5
6
7
8
9
|
JsonSerializerSettings settings = new JsonSerializerSettings(); // 设置日期格式 settings.DateFormatString = "yyyy-MM-dd" ; // 忽略空值 settings.NullValueHandling = NullValueHandling.Ignore; // 缩进 settings.Formatting = Formatting.Indented; JsonConvert.SerializeObject(person, settings); |
返回:
1
2
3
4
5
6
7
8
9
10
11
|
{ "ID" : 1, "Name" : "张三" , "Birthday" : "2000-01-02" , "IsVIP" : true , "Account" : 12.34, "Favorites" : [ "吃饭" , "睡觉" ] } |
1.4 Json 反序列化
1
|
JsonConvert.DeserializeObject<Person>(json); |
2 JObject 使用
2.1 创建对象
1
2
3
4
5
6
7
8
9
10
11
12
|
JObject obj = new JObject(); obj.Add( "ID" , 1); obj.Add( "Name" , "张三" ); obj.Add( "Birthday" , DateTime.Parse( "2000-01-02" )); obj.Add( "IsVIP" , true ); obj.Add( "Account" , 12.34f); // 创建数组 JArray array = new JArray(); array.Add( new JValue( "吃饭" )); array.Add( new JValue( "睡觉" )); obj.Add( "Favorites" , array); obj.Add( "Remark" , null ); |
2.2 JObject 中添加数组
上例中的代码可以简化为:
1
|
JArray array = new JArray( "吃饭" , "睡觉" ); |
2.3 从 Json 字符串创建 JObject
1
2
3
|
string json = "{\"ID\":1,\"Name\":\"张三\",\"Birthday\":\"2000-01-02T00:00:00\",\"IsVIP\":true,\"Account\":12.34,\"Favorites\":[\"吃饭\",\"睡觉\"],\"Remark\":null}" ; JObject obj = JObject.Parse(json); |
2.4 从 Entity 创建 JObject
1
|
JObject obj = JObject.FromObject(person); |
用匿名对象创建 JObject
1
2
3
4
5
6
7
|
JObject obj = JObject.FromObject( new { name = "jack" , age = 18 }); //显示 { "name" : "jack" , "age" : 18 } |
用初始化器
1
2
3
4
5
|
JObject obj = new JObject() { { "name" , "jack" }, { "age" , 18 } }; |
#21b611ff951f9ef061490f6f234ef4bc#
2.5 获取值
1
2
3
|
int id; if (obj[ "ID" ] != null ) id = obj[ "ID" ].Value< int >(); |
2.6 获取数组
Newtonsoft.Json.Linq 不支持直接获取数组,但是可以获取 List,然后再转化为数组。
1
2
3
|
string [] favorites; if (obj[ "Favorites" ] != null ) favorites = obj[ "Favorites" ].Value<List< string >>().ToArray(); |
以上就是c# Newtonsoft.Json 常用方法总结的详细内容,更多关于c# Newtonsoft.Json的资料请关注服务器之家其它相关文章!
原文链接:https://www.cnblogs.com/gl1573/p/11660202.html