Last active
February 14, 2025 18:58
-
-
Save frankhu-2021/b6750185b19fd4ada4ba36b099985813 to your computer and use it in GitHub Desktop.
PrettyPrint JSON with .NET using Newtonsoft.Json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Newtonsoft.Json; | |
public static string JsonPrettify(string json) | |
{ | |
using (var stringReader = new StringReader(json)) | |
using (var stringWriter = new StringWriter()) | |
{ | |
var jsonReader = new JsonTextReader(stringReader); | |
var jsonWriter = new JsonTextWriter(stringWriter) { Formatting = Formatting.Indented }; | |
jsonWriter.WriteToken(jsonReader); | |
return stringWriter.ToString(); | |
} | |
} |
Or
JsonConvert.SerializeObject(JsonConvert.DeserializeObject(json), Formatting.Indented)
Or JsonConvert.SerializeObject(JsonConvert.DeserializeObject(json), Formatting.Indented)
Hehe, this is a more efficient and simpler solution.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for shared