[英].NET - Omit Fields from JSON Web Service Call
我正在为我的公司开发.NET应用程序,以与Nutshell CRM进行交互。 他们在此处提供了有关JSON API的文档。 我正在慢慢构建应用程序中的所有类,但是遇到了只需要更新调用中一个字段的问题,但是让应用程序包含了该类中我拥有的每个字段。
因此,对于editLead方法的精简示例,我仅在其中修改customFields
:
果壳文档指出所有字段都是可选的。 我的课程设置如下,其中我在Nutshell中的自定义字段是部门,产品,国家/地区:
public class editLead
{
public Customfields customFields { get; set; }
}
public class Customfields
{
public string Division { get; set; }
public string Product { get; set; }
public string Country { get; set; }
}
编辑(添加更多代码):
[DataContract(Name = "params")]
public class EditLeadParams
{
public string leadId { get; set; }
public editLead lead { get; set; }
public string rev { get; set; }
}
我正在使用RestSharp进行以下调用:
var editleadclient = new RestClient();
Method editleadMethod = new Method();
editleadMethod = Method.POST;
var editleadrequest = new RestRequest(editleadMethod);
editleadrequest.RequestFormat = DataFormat.Json;
editleadclient.BaseUrl = new Uri(apiuri);
editleadrequest.Credentials = new NetworkCredential(login, apikey);
leadJSON.EditLeadParams lead1 = new leadJSON.EditLeadParams()
{
leadId = foundlead[0],
lead = new leadJSON.editLead()
{
customFields = new leadJSON.Customfields()
{
Division = "AMERICAS",
}
},
rev = foundlead[1],
};
leadJSON.EditLeadRequest editreq = new leadJSON.EditLeadRequest()
{
@params = lead1,
method = "editLead",
};
editleadrequest.AddBody(editreq);
IRestResponse editResponse = editleadclient.Execute(editleadrequest);
如果我只想更新该部门,它将使用以下JSON {"customFields":{"Division":"AMERICAS","Product":null,"Country":null}}
,并覆盖Product和Country字段并使其空白。 但是,如果我在Customfields
定义中注释了Product
and Country
,它将更新该Division
,而仅留下Product
and Country
。
还有另一种定义这些类的方法,以便可以全部定义,但仅更新需要的内容吗?
宣言:
//JsonSerializer.cs
public static class JsonSerializer
{
public static string Serialize(object target, bool ignoreNulls = true)
{
var javaScriptSerializer = new JavaScriptSerializer();
if (ignoreNulls)
{
javaScriptSerializer.RegisterConverters(new[]
{
new NullExclusionConverter(target)
});
}
return javaScriptSerializer.Serialize(target);
}
}
//NullExclusionConverter.cs
public class NullExclusionConverter : JavaScriptConverter
{
private readonly Type _type;
public NullExclusionConverter(object target)
{
if (target == null)
{
throw new ArgumentNullException("target");
}
this._type = target.GetType();
}
public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
{
throw new NotImplementedException();
}
public override IEnumerable<Type> SupportedTypes
{
get
{
return new[] { this._type };
}
}
public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
{
var result = new Dictionary<string, object>();
if (obj == null)
{
return result;
}
var properties = obj.GetType().GetProperties();
foreach (var propertyInfo in properties)
{
//Use propertyInfo.Name to exclude a specific property name
if (propertyInfo.GetValue(obj, null) == null)
{
continue;
}
result.Add(propertyInfo.Name, propertyInfo.GetValue(obj, null));
}
return result;
}
}
用法:
string jsonString = JsonSerializer.Serialize(objectToSerialize);
添加对System.Web.Extensions的引用
我离开了我的最初答案,因为它只返回非空属性作为Json字符串。 但是,这是使用RestSharp时的答案。
根据您的要求添加:
editleadrequest.JsonSerializer.Options = new SerializerOptions()
{
SkipNullProperties = true
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.