JProperty 不会为值生成双引号

JProperty does not generate double quotes for value

我正在尝试创建一个不基于任何 class 或对象的 JSON 响应。它本质上是非常动态的。因此我开始使用 Newtonsoft.Json.Linq 中的 JObject。虽然它生成了正确的结构,但它没有用双引号将值封装起来。 如何在值周围强制使用这些双引号?

这是我用来测试的一小段代码:

var job = new JObject();
job.Add(new JProperty("name", "filip"));
string nm = "Rob";
job.Add(new JProperty("name2", nm));
job.Add(new JProperty("name4", new JValue("Samantha")));

结果:

{"name":filip,"name2":Rob,"name4":Samantha}

我的期望:

{"name":"filip","name2":"Rob","name4":"Samantha"}

这是一个完整的例子:

public class DynJSonService : NancyModule
{
   public DynJSonService()
   {
      Get["/dynjson"] = _ =>
      {
         var job = new JObject();
         job.Add(new JProperty("name", "filip"));
         string nm = "Rob";
         job.Add(new JProperty("name2", nm));
         job.Add(new JProperty("name4", new JValue("Samantha")));
         return job;
      };
   }
}

当浏览到 url 时,浏览器会给出以下响应:localhost:4439/FilipsApps/dynjson

{"name":filip,"name2":Rob,"name4":Samantha}

Nancy 使用 SimpleJson serializer internally by default, which does not know how to handle JObject and JProperty properly. If you want to use those, you should configure Nancy to use the Json.Net (a.k.a. Newtonsoft.Json) serializer instead. There is a Nancy.Serialization.JsonNet NuGet package for that. After you install that package, it should work correctly. If you need to customize the settings for the Json.Net serializer, there are instructions in the readme.md 该包。