【发布时间】:2018-10-12 12:32:33
【问题描述】:
我从 web api 调用中得到这样的响应文本
{
"response":{"numFound":4661,"start":0,"maxScore":6.3040514,"docs":[..] }
}
我正在尝试像这样反序列化它
var result = JsonConvert.DeserializeObject<RootObject>(responseBodyAsText);
这是我的 c# 类:
public class Doc
{
public string id { get; set; }
public string journal { get; set; }
public string eissn { get; set; }
public DateTime publication_date { get; set; }
public string article_type { get; set; }
public List<string> author_display { get; set; }
public List<string> @abstract { get; set; }
public string title_display { get; set; }
public double score { get; set; }
}
public class Response
{
public int numFound { get; set; }
public int start { get; set; }
public double maxScore { get; set; }
public List<Doc> docs { get; set; }
}
public class RootObject
{
public Response response { get; set; }
}
完整的源代码:
namespace CrossSampleApp1.Common
{
public class ServiceManager<T>
{
public delegate void SucessEventHandler(T responseData, bool
HasMoreRecords = false);
public delegate void ErrorEventHandler(ErrorData responseData);
public event SucessEventHandler OnSuccess;
public event ErrorEventHandler OnError;
public async Task JsonWebRequest(string url, string contents, HttpMethod methodType, string mediaStream = "application/json")
{
bool isSuccessRequest = true;
string responseBodyAsText = string.Empty;
try
{
HttpClientHandler handler = new HttpClientHandler();
using (HttpClient httpClient = new HttpClient(handler))
{
HttpRequestMessage message = new HttpRequestMessage(methodType, url);
if (methodType == HttpMethod.Post)
{
message.Headers.ExpectContinue = false;
message.Content = new StringContent(contents);
message.Content.Headers.ContentLength = contents.Length;
message.Content.Headers.ContentType = new MediaTypeHeaderValue(mediaStream);
}
httpClient.Timeout = new TimeSpan(0, 0, 10, 0, 0);
HttpResponseMessage response = await httpClient.SendAsync(message);
response.EnsureSuccessStatusCode();
responseBodyAsText = response.Content.ReadAsStringAsync().Result;
}
}
catch (HttpRequestException hre)
{
//responseBodyAsText = "Exception : " + hre.Message;
responseBodyAsText = "Can't Connect (Please check your network connection)";
isSuccessRequest = false;
}
catch (Exception ex)
{
// responseBodyAsText = "Exception : " + ex.Message;
responseBodyAsText = "Can't Connect (Please check your network connection)";
isSuccessRequest = false;
}
try
{
if (isSuccessRequest)
{
if (typeof(T) == typeof(string))
{
OnSuccess?.Invoke((T)(object)responseBodyAsText);
}
else if (typeof(T) == typeof(ServiceResponse))
{
T result = JsonConvert.DeserializeObject<T>(responseBodyAsText);
OnSuccess?.Invoke(result);
}
else
{
var result = JsonConvert.DeserializeObject<RootObject>(responseBodyAsText);
var data = JsonConvert.DeserializeObject<T>(Convert.ToString(result));
OnSuccess?.Invoke(data);
}
}
else
{
OnError?.Invoke(new ErrorData
{
ErrorText = responseBodyAsText
});
}
}
catch (Exception e)
{
OnError?.Invoke(new ErrorData
{
ErrorText = e.Message
});
}
}
}
public class ErrorData : EventArgs
{
public string ErrorText { get; set; }
public bool Status { get; set; }
}
}
但我在结果中得到空值。任何人都可以帮忙。我做错了什么
谢谢。
【问题讨论】:
-
您实际上是否在某处反序列化为
RootObject类型?还是您尝试使用as投射它? -
请提供一个minimal reproducible example,因为给定的代码给了我一个非空的
result值,这与你所说的相反。 -
仅供参考,您实际上并没有使用您提到的类,因为您使用的是
.DeserializeObject的非通用版本,这意味着您将获得@987654331 @。如果您稍后拥有result as RootObject,那么是的,您将获得null。然而,这就是为什么我们需要minimal reproducible example,以便确切地了解您在做什么。 -
@Neelam 最好不要将您的代码作为引号。
-
var data = JsonConvert.DeserializeObject<T>(Convert.ToString(result));呃,除非RootObject的ToString()方法被重载以返回一个JSON 对象,那么Convert.ToString()不会按照您的想法做。Convert.ToString(instanceOfRootObject)将返回“CrossSampleApp1.Common.RootObject”(如果 RootObject 在“CrossSampleApp1.Common”命名空间中)
标签: c# xamarin xamarin.forms json.net deserialization