C# Newtonsoft.Json.JsonConvert.SerializeObject() 参数Newtonsoft.Json.Formatting.Indented与不带参数的区别。
Newtonsoft.Json.JsonConvert.SerializeObject()这个函数返回一个JSON字符串。
默认的,参数是None,如果加了Newtonsoft.Json.Formatting.Indented,会返回标准的格式化后的JSON字符串。
写了个小Demo测试了一携带参数和不带参数的区别
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
namespace Demo
{
class Person
{
public string name = null;
public string age = null;
}
class MainClass
{
public static void Main(string[] args)
{
Person p1 = new Person();
p1.name = "name1";
p1.age = "age1";
Person p2 = new Person();
p2.name = "name2";
p2.age = "age2";
string JSON = Newtonsoft.Json.JsonConvert.SerializeObject(p1, Newtonsoft.Json.Formatting.Indented);
Console.WriteLine(JSON);
string JSON1 = Newtonsoft.Json.JsonConvert.SerializeObject(p2);
Console.WriteLine(JSON1);
}
}
}
- 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
打印结果如下:
{
"name": "name1",
"age": "age1"
}
{"name":"name2","age":"age2"}
- 1
- 2
- 3
- 4
- 5
Newtonsoft.Json.Formatting.Indented 表示 ”缩进“,即返回前面的打印结果。默认是返回后面的打印结果。
