C# 我反序列化哪里出了问题

C# 我反序列化哪里出了问题,c#,json,api,C#,Json,Api,我正在尝试获取有关stackoverflow的问题总数,因为我正在做一个练习,并且一直在进行反序列化 如果有人能让我知道我需要做什么,我将不胜感激,也许能提供一些示例代码来帮助我 当我在控制台中运行这个时,我得到了错误 “反序列化类型的对象时出错。遇到意外字符”,该字符是向下指向的三角形 这就是我目前所拥有的 using System; using System.Threading.Tasks; using System.Net; using System.Net.Http; using Sys

我正在尝试获取有关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);
        }
    }
}

它返回的是压缩内容(特别是gzip)。如果可能,请告诉您的http客户端自动解压缩:

request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
request.AutomaticDecompression =
    DecompressionMethods.GZip | DecompressionMethods.Deflate;
下一个问题是:模式不匹配;问题在根目录的
项中:

[DataContract]
public class Response
{
    [DataMember(Name ="items")]
    List<Question> questions { get;set; }
}
[DataContract]
公众课堂反应
{
[DataMember(Name=“items”)]
列出问题{get;set;}
}

您是否考虑过简单地使用StacMan或其他现有库?我不知道SO api,也从未遇到过“向下三角形…”,但我建议您使用NewtonSoft.Json库进行Json反序列化。此url的响应与响应类的属性不匹配。您建议这样做的原因是什么?我对这类请求的经验非常有限。@SimonPrice紧接着
GetResponse()
@SimonPrice之前,在这种情况下,我强烈建议使用类似stacman的工具来更改[DataContract]公共类响应{[DataMember(Name=“items”)]列表问题{get;set;}这会引发类型或命名空间名称列表无法找到的错误found@SimonPrice如果您愿意,请使用
问题[]
;p
List
只需要在表单顶部使用
指令:
使用System.Collections.Generic