. net -省略JSON Web服务调用中的字段
本文关键字:调用 字段 服务 Web 省略 JSON net | 更新日期: 2023-09-27 18:15:06
我正在为我的公司开发一个。net应用程序,以便与Nutshell CRM进行交互。他们在这里为JSON API提供了文档。我正在慢慢地构建应用程序中的所有类,但是我遇到了只需要在调用中更新一个字段的问题,但是让应用程序包含我在该类上拥有的每个字段。
对于editLead方法的浓缩示例,我只修改customFields
:
Nutshell文档说明所有字段都是可选的。我的类设置如下,其中我在Nutshell中的自定义字段是Division, Product, Country:
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; }
}
EDIT(添加更多代码):
[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);
如果我只想更新Division,它将使用以下JSON {"customFields":{"Division":"AMERICAS","Product":null,"Country":null}}
,并覆盖Product和Country字段并使其为空。但是,如果我在Customfields
定义中注释掉Product
和Country
,它将更新Division
,而单独保留Product
和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
};