سؤال

Using the new System.Json from 4.5 or the equivalent System.Json for 4.0 from Nuget how do you format the output so it is indented ans spaced in a more readable form?

So this

dynamic jsonObj = new JsonObject();
jsonObj.firstName = "John";
jsonObj.lastName = "Smith";
Debug.WriteLine((string)jsonObj.ToString());

Outputs this

{"firstName":"John","lastName":"Smith"}

When I want this

{
  "firstName": "John",
  "lastName": "Smith"
}
هل كانت مفيدة؟

المحلول

For future reference, the System.Json library in .NET 4.5 (And 4.5 only, not Silverlight) has the JsonSaveOptions enumerator, so you can call ToString(JsonSaveOptions.EnableIndent) for pretty printed Json.

نصائح أخرى

Unlike with XML, there's there are no options for this in the built-in library.

Mark Rogers wrote a prettifier, available here:

http://www.markdavidrogers.com/json-pretty-printerbeautifier-library-for-net/

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top