使用LINQ创建JSON时的JSON.NET参数异常

时间:2012-10-29 09:06:58

标签: linq json.net

我正在使用JSON.NET Linq到JSON,当我执行下面的命令时,我得到以下参数异常:

  

无法将Newtonsoft.Json.Linq.JValue添加到Newtonsoft.Json.Linq.JObject

代码有什么问题

string content = new JObject(
            new JObject("auth",
                        new JProperty("user", "anemail@gmail.com"),
                        new JProperty("secret", "somepassword")
                ),
            new JObject("config",
                        new JProperty("template", "1")
                ),
            new JObject("data",
                        new JProperty("email", "body")
                )
            ).ToString();

1 个答案:

答案 0 :(得分:2)

根据JSON.NET documentation,您需要将您的JObject包装在JProperties中:


string content = new JObject(
                   new JProperty("auth",
                     new JObject(
                       new JProperty("user", "anemail@gmail.com"),
                       new JProperty("secret", "somepassword")
                     )
                   ),
                   new JProperty("config",
                     new JObject(
                       new JProperty("template", "1")
                     )
                   ),
                   new JProperty("data",
                     new JObject(
                       new JProperty("email", "body")
                     )
                   )
                 ).ToString();