首先,我知道它不是有效的JSON格式,但这是我的客户Web服务设置,我需要将 Json.Net JsonTextWriter的QuoteName设置为false,但我不知道如何正确执行此操作。这是我的尝试:
foreach (var c in a)
{
var jsonobjects = JsonConvert.SerializeObject(c, new JsonTextWriter
{
QuoteName = false
}
);
//Some code that using serialized json object
}
为了澄清我想要什么。我需要有不带引号的 Json 属性名称。类似的东西:{属性:"值"}。
下面是
一个示例:
班级:
public class Address
{
public string Street { get; set; }
public uint Number { get; set; }
public string City { get; set; }
public string ZipCode { get; set; }
public string State { get; set; }
public string Country { get; set; }
}
序列化逻辑:
var address = new Address
{
Street = "Fulton Street",
Number = 100,
City = "New York",
ZipCode = "10038",
State = "NY",
Country = "USA"
};
var serializer = new JsonSerializer();
var stringWriter = new StringWriter();
using (var writer = new JsonTextWriter(stringWriter))
{
writer.QuoteName = false;
serializer.Serialize(writer, address);
}
var json = stringWriter.ToString();
Console.WriteLine(json);
输出:
{
Street: "Fulton Street",
Number: 100,
City: "New York",
ZipCode: "10038",
State: "NY",
Country: "USA"
}
例:
https://dotnetfiddle.net/zazT1T