네임스페이스 Newtonsoft.Json.Formatting Newtonsoft.Json.Formatting 사용 Json을 포맷하기 위한 포맷 옵션 제공
없음 − 특별한 서식이 적용되지 않습니다. 이것이 기본값입니다.
들여쓰기 − 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"}