
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How to get formatted JSON in .NET using C#?
Use Namespace Newtonsoft.Json.Formatting Newtonsoft.Json.Formatting provides formatting options to Format the Json
None − No special formatting is applied. This is the default.
Indented − Causes child objects to be indented according to the Newtonsoft.Json.JsonTextWriter.Indentation and Newtonsoft.Json.JsonTextWriter.IndentChar settings.
Example
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; } }
Output
{ "Sizes": [ "Small", "Medium", "Large" ], "Price": 3.9900, "Expiry": "2008-12-28T00:00:00", "Name": "Apple" }
Example
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; } }
Output
{"Sizes":["Small","Medium","Large"],"Price":3.9900,"Expiry":"2008-12-28T00:00:00","Name":"Apple"}
- Related Articles
- How to work with XML and JSON in .NET?
- How to get formatted date and time in Python?
- How to Deserializing JSON to .NET object using Newtonsoft json in C# and pick only one value from the array?
- How to get a JSON field in a nested JSON using Rest Assured?
- How to get a JSON array field in a nested JSON using Rest Assured?
- How to calculate net present value using Net present value (NPV)?
- How to print a formatted text using printf() method in Java?\n
- How to get JSON fields(nodes) based on conditions using Rest Assured?
- How to get all the keys of a JSON object using GSON in Java?
- Formatted text in Linux Terminal using Python
- Formatted Strings Using Template Strings in JavaScript
- How to add elements to JSON Object using JSON-lib API in Java?
- How to convert a Collection to JSON Array using JSON-lib API in Java?
- How to convert a Map to JSON object using JSON-lib API in Java?
- How to convert JSON string to array of JSON objects using JavaScript?

Advertisements