नेमस्पेस 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"}