-1

I have several JSon properties, I want to put a new line between them.
I read other posts, they are suggesting escape chars but it didn't work.

My txt file look like this:

{"flag":false,"flag2":false,"SET TRUE USED":false,"SET FALSE USED":false}

I want to list them like:

{
    "flag":false,
    "flag2":false,
    "SET TRUE USED":false,
    "SET FALSE USED":false
}

Model:

class Class1
{
    [JsonProperty(PropertyName = "flag")]
    public bool flag { get; set; }

    [JsonProperty(PropertyName = "flag2")]
    public bool flag2 { get; set; }

    [JsonProperty(PropertyName = "SET TRUE USED")]
    public bool M1 { get; set; }

    [JsonProperty(PropertyName = "SET FALSE USED")]
    public bool M2 { get; set; }
}
Peter B
  • 22,460
  • 5
  • 32
  • 69
B B
  • 121
  • 9
  • 1
    The question is why do you need this? I ask this because you also include a class in your question. Are you doing some (de)-serialization? Depending on what you realy need, the solution might be different. – Aldert Jul 19 '19 at 09:11
  • @PeterWolf I already checked it and applied "dynamic parsedJson = JsonConvert.DeserializeObject(json);" it didn't work – B B Jul 19 '19 at 09:13
  • Unclear what you are asking. Looks like an X/Y problem, are you looking to pretty print/prettyfy a condensed JSON? https://stackoverflow.com/questions/4580397/json-formatter-in-c – xdtTransform Jul 19 '19 at 09:13
  • @Aldert yes I am doing (de)-serialization(both) – B B Jul 19 '19 at 09:14
  • When you deserialize, your original string should work fine, do you get an error? – Aldert Jul 19 '19 at 09:17
  • @Aldert I solved the problem thank you – B B Jul 19 '19 at 09:18
  • @BB If you solve the problem, instead of changing the question, answer to your own post and accepted as the correct answer. Will be easier for new readers to understand the problem and the correct answer to solve it. – Ricardo Rocha Jul 19 '19 at 09:24

1 Answers1

2

You need to add Formatting.Indented:

string jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(
    Class1,
    Newtonsoft.Json.Formatting.Indented);
Matt
  • 1,245
  • 2
  • 17
  • 32