生成具有NewtonSoft JSON对象在单行 [英] Generate JSON object with NewtonSoft in a single line

查看:279
本文介绍了生成具有NewtonSoft JSON对象在单行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用JSON库 NewtonSoft 生成一个JSON字符串:

  JObject OUT = JObject.FromObject(新
{
典型值=照片
});

返回out.ToString();



输出:

  {
典型:照片
}

我的问题:
是否有可能得到输出像一行:

  {典型: 照片} 


解决方案

您可以使用的过载< A HREF =htt​​p://james.newtonking.com/projects/json/help/html/M_Newtonsoft_Json_Linq_JToken_ToString_1.htm> JObject.ToString() 这需要 格式 为参数:

  JObject的obj = JObject.FromObject(新
{
典型值=照片
});

返回obj.ToString(Formatting.None);


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"}

解决方案

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);

这篇关于生成具有NewtonSoft JSON对象在单行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆