Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate JSON object with NewtonSoft in a single line

Tags:

json

c#

json.net

I'm using the JSON library NewtonSoft to generate a JSON string:

JObject out = JObject.FromObject(new             {                 typ = "photos"             });              return out.ToString(); 

Output:

{   "typ": "photos" } 

My question: Is it possible to get the output in a single line like:

{"typ": "photos"} 
like image 557
Calimero Avatar asked Dec 17 '12 15:12

Calimero


2 Answers

You can use the overload of JObject.ToString() which takes Formatting as parameter:

JObject obj = JObject.FromObject(new {     typ = "photos" });  return obj.ToString(Formatting.None); 
like image 140
tpeczek Avatar answered Sep 18 '22 06:09

tpeczek


var json = JsonConvert.SerializeObject(new { typ = "photos" }, Formatting.None); 
like image 39
L.B Avatar answered Sep 20 '22 06:09

L.B