CSharp · Console
using System;
using System.IO;
using Newtonsoft.Json;
public class Program
{
public static void Main()
{
string json = @"
{
""notes"": [
{
""__type__"": ""Note"",
""data"": """",
""fields"": [
""Define virologic suppression in HIV"",
""HIV RNA level below limit of assay detection (<20copies/mL"",
"""",
""""
],
""flags"": 0,
""guid"": ""B4VR%D9qb2"",
""note_model_uuid"": ""0aecb5c4-d637-11e7-8e91-408d5c5c9416"",
""tags"": [
""InfectiousDiseases""
]
},
{
""__type__"": ""Note"",
""data"": """",
""fields"": [
""Define incomplete virological response in HIV"",
""Inability to achieve virologic suppression after 24 weeks of therapy (viral load >200 on 2 consecutive samples)"",
"""",
""""
],
""flags"": 0,
""guid"": ""N{WL%;*sKr"",
""note_model_uuid"": ""0aecb5c4-d637-11e7-8e91-408d5c5c9416"",
""tags"": [
""InfectiousDiseases""
]
}
]
}";
Console.WriteLine(ReformatJson(json));
}
public static string ReformatJson(string json)
{
using (StringReader sr = new StringReader(json))
using (JsonReader reader = new JsonTextReader(sr))
using (StringWriter sw = new StringWriter())
using (JsonWriter writer = new DoubleSpacedValuesJsonWriter(sw))
{
writer.WriteToken(reader);
return sw.ToString();
}
}
}
public class DoubleSpacedValuesJsonWriter : JsonTextWriter
{
public DoubleSpacedValuesJsonWriter(TextWriter textWriter) : base(textWriter)
{
Formatting = Formatting.Indented;
}
protected override void WriteValueDelimiter()
{
base.WriteValueDelimiter();
base.WriteWhitespace(Environment.NewLine);
}
}