Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to turn on indentation when writing JSON using Json.net?

Tags:

json

c#

json.net

I am using Json.Net to serialize XML into Json . When i write the serialized string to a file it all comes in a single line . How do i get it to actually look like Json with the usual tabs and indentation?

like image 360
ashutosh raina Avatar asked Oct 30 '11 18:10

ashutosh raina


People also ask

What is formatting indented?

In a composition, an indentation is a blank space between a margin and the beginning of a line of text. The beginning of this paragraph is indented. Standard paragraph indentation is about five spaces or one-quarter to one-half of an inch, depending on which style guide you follow.

What is Jsonconvert SerializeObject C#?

SerializeObject Method (Object, Type, JsonSerializerSettings) Serializes the specified object to a JSON string using a type, formatting and JsonSerializerSettings. Namespace: Newtonsoft.Json.

What is JsonSerializerSettings?

Specifies the settings on a JsonSerializer object. Newtonsoft.Json. JsonSerializerSettings. Namespace: Newtonsoft.Json.


1 Answers

Set the JSON writer Formatting property to Formatting.Indented:

jsonWriter.Formatting = Formatting.Indented; 

The JsonConvert.Serialize* methods also have overloads that take a Formatting enum (thanks John Flatness).

Documentation: Serialize an Object

like image 124
Oded Avatar answered Oct 10 '22 10:10

Oded