Wei Lin
Wei Lin

Reputation: 3811

How Visual Studio Immediate Windows to print format indented JSON?

I tried to use Newtonsoft.Json.JsonConvert.SerializeObject(rows, Formatting.Indented) to convert variable to json when debugging, but vs immediate only show non-format string like image

I expect to get result like below LINQPad's result

Upvotes: 0

Views: 2854

Answers (3)

sairfan
sairfan

Reputation: 1062

Another approach could be saving json to file, it will greatly help to analyze and compare different outputs

System.IO.File.WriteAllText(@"c:\temp\debug_file.json", Newtonsoft.Json.JsonConvert.SerializeObject(tSet, Newtonsoft.Json.Formatting.Indented));

Upvotes: 0

Christian May
Christian May

Reputation: 326

If you would like to print newline and tab characters, you can type the variable name into the Immediate Window followed by the string ",nq".

So instead of typing Newtonsoft.Json.JsonConvert.SerializeObject(rows, Formatting.Indented) into the Immediate Window, you would type Newtonsoft.Json.JsonConvert.SerializeObject(rows, Formatting.Indented),nq.

The ",nq" at the end serves as a "format specifier" for the Immediate Window. Find out more about that here.

Thanks to user davesem for pointing this out here.

Upvotes: 2

JL0PD
JL0PD

Reputation: 4518

Personally I'm not using immediate window because find it less useful than other options.

To inspect value you can hover over variable and click on magnifier icon, it will display stored data with ToString() representation. Optionally you can choose other visualizer like json, xml or html with dropdown near icon. Downside is that you need declared variable.

enter image description here

Other option is to use Watch panel (during debug click debug -> windows -> watch -> watch 1). It allows you to inspect variables, override them and call methods

enter image description here

Upvotes: 1

Related Questions