1

我正在使用 JSON.NET Linq to 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();
4

1 回答 1

2

根据 JSON.NET文档,您需要将 JObjects 自己包装在 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();

于 2012-10-29T12:04:57.160 回答