如何使用C#在.NET中获取格式化的JSON?
使用命名空间Newtonsoft.Json.FormattingNewtonsoft.Json.Formatting提供了用于格式化Json的格式化选项
None-不应用特殊格式。这是默认值。
Indented-使子对象根据Newtonsoft.Json.JsonTextWriter.Indentation和Newtonsoft.Json.JsonTextWriter.IndentChar设置缩进。
示例
static void Main(string[] args){
Product product = new Product{
Name = "Apple",
Expiry = new DateTime(2008, 12, 28),
Price = 3.9900M,
Sizes = new[] { "Small", "Medium", "Large" }
};
string json = JsonConvert.SerializeObject(product, Formatting.Indented);
Console.WriteLine(json);
Product deserializedProduct = JsonConvert.DeserializeObject<Product>(json);
Console.ReadLine();
}
class Product{
public String[] Sizes { get; set; }
public decimal Price { get; set; }
public DateTime Expiry { get; set; }
public string Name { get; set; }
}输出结果
{
"Sizes": [
"Small",
"Medium",
"Large"
],
"Price": 3.9900,
"Expiry": "2008-12-28T00:00:00",
"Name": "Apple"
}示例
static class Program{
static void Main(string[] args){
Product product = new Product{
Name = "Apple",
Expiry = new DateTime(2008, 12, 28),
Price = 3.9900M,
Sizes = new[] { "Small", "Medium", "Large" }
};
string json = JsonConvert.SerializeObject(product, Formatting.None);
Console.WriteLine(json);
Product deserializedProduct = JsonConvert.DeserializeObject<Product>(json);
Console.ReadLine();
}
}
class Product{
public String[] Sizes { get; set; }
public decimal Price { get; set; }
public DateTime Expiry { get; set; }
public string Name { get; set; }
}输出结果
{"Sizes":["Small","Medium","Large"],"Price":3.9900,"Expiry":"2008-12-28T00:00:00","Name":"Apple"}热门推荐
10 写给兄弟的祝福语简短
11 圣诞简短祝福语给父母
12 住学长学姐祝福语简短
13 今年节日祝福语简短
14 中秋祝福语简短版大全
15 档案调动孩子祝福语简短
16 员工对同事祝福语简短
17 毕业英语祝福语大全简短
18 鱼的内涵简短祝福语