我正在尝试获取有关我正在进行的练习的stackoverflow上的问题总数,并且我坚持反序列化。
如果有人可以让我知道我需要做什么,我将不胜感激,或许可以提供一些可以帮助我的示例代码。
当我在控制台中运行它时,我收到错误 “反序列化类型对象时出错。遇到意外字符”这是一个向下指向的三角形......
这是我到目前为止所拥有的
using System;
using System.Threading.Tasks;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
namespace cs_StackOverflowAPI
{
[DataContract(Namespace = "http://schemas.microsoft.com/search/local/ws/rest/v1")]
public class Question
{
[DataMember(Name = "question_id")]
public int Questions { get; set; }
}
[DataContract]
public class ResourceSet
{
[DataMember(Name = "estimatedTotal")]
public long EstimatedTotal { get; set; }
[DataMember(Name = "resources")]
public Question[] Resources { get; set; }
}
[DataContract]
public class Response
{
[DataMember(Name = "statusCode")]
public int StatusCode { get; set; }
[DataMember(Name = "statusDescription")]
public string StatusDescription { get; set; }
[DataMember(Name = "authenticationResultCode")]
public string AuthenticationResultCode { get; set; }
[DataMember(Name = "errorDetails")]
public string[] errorDetails { get; set; }
[DataMember(Name = "traceId")]
public string TraceId { get; set; }
[DataMember(Name = "resourceSets")]
public ResourceSet[] ResourceSets { get; set; }
}
class Program
{
public string serverUrl = "https://api.stackexchange.com/";
public HttpClient client = null;
static void Main(string[] args)
{
try
{
string noOfQuestions = CreateRequest("questions");
Response noOfQuestionsReponse = MakeRequest(noOfQuestions);
ProcessResponse(noOfQuestionsReponse);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.Read();
}
}
public static string CreateRequest(string queryString)
{
string UrlRequest = "https://api.stackexchange.com/2.2/" +
queryString +
"?&site=stackoverflow";
return (UrlRequest);
}
public static Response MakeRequest(string requestUrl)
{
try
{
HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
if (response.StatusCode != HttpStatusCode.OK)
throw new Exception(String.Format(
"Server error (HTTP {0}: {1}).",
response.StatusCode,
response.StatusDescription));
DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(Response));
object objResponse = jsonSerializer.ReadObject(response.GetResponseStream());
Response jsonResponse
= objResponse as Response;
return jsonResponse;
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return null;
}
}
static public void ProcessResponse(Response noOfQuestionsReponse)
{
// this is where the error occurs
Console.WriteLine("Press any key to exit");
Console.ReadKey();
}
public static long ToUnixTime(DateTime date)
{
var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
return Convert.ToInt64((date.ToUniversalTime() - epoch).TotalSeconds);
}
}
}
答案 0 :(得分:1)
返回压缩内容(特别是gzip)。如果可能,请告诉您的http客户端自动解压缩它:
request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
request.AutomaticDecompression =
DecompressionMethods.GZip | DecompressionMethods.Deflate;
下一个问题是:架构不匹配;问题在根目录的items
中:
[DataContract]
public class Response
{
[DataMember(Name ="items")]
List<Question> questions { get;set; }
}