我目前正在使用一個控制檯應用程序,我正在使用HttpClient與Apache CouchDB數據庫進行交互。我用這篇文章:http://www.asp.net/web-api/overview/web-api-clients/calling-a-web-api-from-a-net-client如何讓HttpClient Json序列化程序忽略空值
我想,當我序列化和通過PostAsJsonSync將文檔發送到我的數據庫忽略班裏空的屬性,但我不知道如何:
public static HttpResponseMessage InsertDocument(object doc, string name, string db)
{
HttpResponseMessage result;
if (String.IsNullOrWhiteSpace(name)) result = clientSetup().PostAsJsonAsync(db, doc).Result;
else result = clientSetup().PutAsJsonAsync(db + String.Format("/{0}", name), doc).Result;
return result;
}
static HttpClient clientSetup()
{
HttpClientHandler handler = new HttpClientHandler();
handler.Credentials = new NetworkCredential("**************", "**************");
HttpClient client = new HttpClient(handler);
client.BaseAddress = new Uri("*********************");
//needed as otherwise returns plain text
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
return client;
}
這裏是我序列化類....
class TestDocument
{
public string Schema { get; set; }
public long Timestamp { get; set; }
public int Count { get; set; }
public string _rev { get; set; }
public string _id { get; set; } - would like this to be ignored if null
}
任何幫助非常讚賞。
在幕後,你很可能使用Json.NET。 http://stackoverflow.com/questions/9819640/json-net-ignoring-null-fields – 2013-03-02 20:39:51
任何想法如何引用它使用的序列化呢? – 2013-03-02 20:44:40
看起來完全像我的問題,當把文檔放到CouchDB中時 – Thomas 2018-01-15 12:55:29