将 JSON 属性映射到自定义 C# class
Mapping JSON Attribute to custom C# class
我正在尝试使用 RestSharp 将 JSON 响应映射到 c# class。我能够正确映射响应中的几乎所有字段,但是有一些字段(即属性字段)被标记为 null
。我想知道我是否需要做一些特别的事情来处理这些情况。
JSON 的 return 看起来像这样:
{
"Response": {
"@VersionNumber": "5.0",
"ResponseDetail": {
"CriteriaText": {
"$": "Name"
}
}
"ResultQuantity": 1,
"Result": [
{
"Id": "1234567890",
"PrimaryName": {
"OrganizationName": {
"$": "CompanyName"
}
}
}
}
}
我能够正确提取所有数据,除了任何属性(以 @ 或 $ 开头)。
这是我尝试映射响应的方式:
public class Response
{
public string VersionNumber { get; set; }
public ResponseDetail ResponseDetail { get; set; }
public Result Result { get; set; }
}
我曾尝试将其添加到版本号 [JsonProperty("@VersionNumber")]
之上,但也没有成功。
这是我的 GET 调用:
public Response GetData(string endpoint)
{
RestRequest request = new RestRequest(Method.GET)
{
RequestFormat = DataFormat.Json,
Resource = _version + endpoint,
RootElement = "Response"
};
request.AddHeader("Authorization", _token);
var response = _client.Execute<Response>(request);
if (response == null || !PassCodes.Contains(response.StatusCode))
{
return null;
}
return response.Data;
}
任何想法都会有所帮助,提前致谢!
要使用 Newtonsoft.Json.JsonProperty
属性,您需要将 RestSharp 从它自己的序列化程序切换到 Newtonsoft.Json。看这段代码为基础:http://bytefish.de/blog/restsharp_custom_json_serializer/.
public class NewtonsoftJsonSerializer : IJsonSerializer
{
private Newtonsoft.Json.JsonSerializer serializer;
public NewtonsoftJsonSerializer(Newtonsoft.Json.JsonSerializer serializer)
{
this.serializer = serializer;
}
public string ContentType {
get { return "application/json"; } // Probably used for Serialization?
set { }
}
public string DateFormat { get; set; }
public string Namespace { get; set; }
public string RootElement { get; set; }
public string Serialize(object obj)
{
using (var stringWriter = new StringWriter())
{
using (var jsonTextWriter = new JsonTextWriter(stringWriter))
{
serializer.Serialize(jsonTextWriter, obj);
return stringWriter.ToString();
}
}
}
public T Deserialize<T>(RestSharp.IRestResponse response)
{
var content = response.Content;
using (var stringReader = new StringReader(content))
{
using (var jsonTextReader = new JsonTextReader(stringReader))
{
return serializer.Deserialize<T>(jsonTextReader);
}
}
}
public static NewtonsoftJsonSerializer Default
{
get
{
return new NewtonsoftJsonSerializer(new Newtonsoft.Json.JsonSerializer()
{
NullValueHandling = NullValueHandling.Ignore,
});
}
}
}
如果使用最新版本的RestSharp,需要添加一个无参ctor:
public NewtonsoftJsonSerializer()
{
this.serializer = new JsonSerializer();
}
并将NewtonsoftJsonSerializer
注册为:
client.AddHandler("application/json", new NewtonsoftJsonSerializer());
然后 JsonProperty
将起作用:
[JsonProperty("@Foo")]
public string Foo { get; set; }
[JsonProperty("$")]
public string Bar { get; set; }
我能够通过简单的字符串替换实现我正在寻找的东西。我删除了任何前导 @ 并将任何单个“$”重命名为 Value。
request.OnBeforeDeserialization = (x =>
{
x.Content = x.Content.Replace("\"@", "\"").Replace("\"$\"","\"Value\"");
});
我正在尝试使用 RestSharp 将 JSON 响应映射到 c# class。我能够正确映射响应中的几乎所有字段,但是有一些字段(即属性字段)被标记为 null
。我想知道我是否需要做一些特别的事情来处理这些情况。
JSON 的 return 看起来像这样:
{
"Response": {
"@VersionNumber": "5.0",
"ResponseDetail": {
"CriteriaText": {
"$": "Name"
}
}
"ResultQuantity": 1,
"Result": [
{
"Id": "1234567890",
"PrimaryName": {
"OrganizationName": {
"$": "CompanyName"
}
}
}
}
}
我能够正确提取所有数据,除了任何属性(以 @ 或 $ 开头)。
这是我尝试映射响应的方式:
public class Response
{
public string VersionNumber { get; set; }
public ResponseDetail ResponseDetail { get; set; }
public Result Result { get; set; }
}
我曾尝试将其添加到版本号 [JsonProperty("@VersionNumber")]
之上,但也没有成功。
这是我的 GET 调用:
public Response GetData(string endpoint)
{
RestRequest request = new RestRequest(Method.GET)
{
RequestFormat = DataFormat.Json,
Resource = _version + endpoint,
RootElement = "Response"
};
request.AddHeader("Authorization", _token);
var response = _client.Execute<Response>(request);
if (response == null || !PassCodes.Contains(response.StatusCode))
{
return null;
}
return response.Data;
}
任何想法都会有所帮助,提前致谢!
要使用 Newtonsoft.Json.JsonProperty
属性,您需要将 RestSharp 从它自己的序列化程序切换到 Newtonsoft.Json。看这段代码为基础:http://bytefish.de/blog/restsharp_custom_json_serializer/.
public class NewtonsoftJsonSerializer : IJsonSerializer
{
private Newtonsoft.Json.JsonSerializer serializer;
public NewtonsoftJsonSerializer(Newtonsoft.Json.JsonSerializer serializer)
{
this.serializer = serializer;
}
public string ContentType {
get { return "application/json"; } // Probably used for Serialization?
set { }
}
public string DateFormat { get; set; }
public string Namespace { get; set; }
public string RootElement { get; set; }
public string Serialize(object obj)
{
using (var stringWriter = new StringWriter())
{
using (var jsonTextWriter = new JsonTextWriter(stringWriter))
{
serializer.Serialize(jsonTextWriter, obj);
return stringWriter.ToString();
}
}
}
public T Deserialize<T>(RestSharp.IRestResponse response)
{
var content = response.Content;
using (var stringReader = new StringReader(content))
{
using (var jsonTextReader = new JsonTextReader(stringReader))
{
return serializer.Deserialize<T>(jsonTextReader);
}
}
}
public static NewtonsoftJsonSerializer Default
{
get
{
return new NewtonsoftJsonSerializer(new Newtonsoft.Json.JsonSerializer()
{
NullValueHandling = NullValueHandling.Ignore,
});
}
}
}
如果使用最新版本的RestSharp,需要添加一个无参ctor:
public NewtonsoftJsonSerializer()
{
this.serializer = new JsonSerializer();
}
并将NewtonsoftJsonSerializer
注册为:
client.AddHandler("application/json", new NewtonsoftJsonSerializer());
然后 JsonProperty
将起作用:
[JsonProperty("@Foo")]
public string Foo { get; set; }
[JsonProperty("$")]
public string Bar { get; set; }
我能够通过简单的字符串替换实现我正在寻找的东西。我删除了任何前导 @ 并将任何单个“$”重命名为 Value。
request.OnBeforeDeserialization = (x =>
{
x.Content = x.Content.Replace("\"@", "\"").Replace("\"$\"","\"Value\"");
});