使用RESTSharp C#反序列化JSONResult

时间:2017-08-21 12:09:45

标签: c# json deserialization restsharp

我正在尝试反序列化JSON,

所以我的请求返回这样的JSON:

{
    "total_records": 5547,
    "total_searched": 4284178,
    "articles": [{
        "rank": 1,
        "content_type": "Conference",
        "article_number": "6482896",
        "doi": "10.1109/EDSSC.2012.6482896",
        "title": "Development of low stress TaN thin film for MEMS/sensor electrode application",
        "publication_number": 6475438,
        "publication_title": "2012 IEEE International Conference on Electron Devices and Solid State Circuit (EDSSC)",
        "isbn": "New-2005_Electronic_978-1-4673-5696-1",
        "publisher": "IEEE",
        "citing_paper_count": 0,
        "index_terms": {
            "ieee_terms": {
                "terms": ["Conductivity", "Electrodes", "Films", "Metals", "Resistance", "Silicon", "Stress"]
            },
            "author_terms": {
                "terms": ["MEMs", "PVD", "TaN", "stress"]
            }
        },
        "pdf_url": "http://ieeexplore.ieee.org/stamp/stamp.jsp?arnumber=6482896",
        "abstract_url": "http://ieeexplore.ieee.org/xpl/articleDetails.jsp?arnumber=6482896",
        "html_url": "http://ieeexplore.ieee.org/xpls/icp.jsp?arnumber=6482896",
        "authors": {
            "authors": [{
                "full_name": "Xiaoxu Kang",
                "author_order": 1
            }, {
                "full_name": "Qingyun Zuo",
                "author_order": 2
            }, {
                "full_name": "Chao Yuan",
                "author_order": 3
            }, {
                "full_name": "Shoumian Chen",
                "author_order": 4
            }, {
                "full_name": "Yuhang Zhao",
                "author_order": 5
            }]
        },
        "conference_location": "Bangkok",
        "conference_dates": "3-5 Dec. 2012",
        "start_page": "1",
        "end_page": "3", ...

但我只想要“作者”部分。所以我试着反序列化它,因此我生成了所有类,这要归功于json2csharp。

    public class IeeeTerms
    {
        public List<string> terms { get; set; }
    }


    public class AuthorTerms
    {
        public List<string> terms { get; set; }
    }


    public class IndexTerms
    {
        public IeeeTerms ieee_terms { get; set; }
        public AuthorTerms author_terms { get; set; }
    }

    public class Author
    {
        public string full_name { get; set; }
        public int author_order { get; set; }
    }

    public class Authors
    {
        public List<Author> authors { get; set; }
    }

    public class Article
    {
        public int rank { get; set; }
        public string content_type { get; set; }
        public string article_number { get; set; }
        public string doi { get; set; }
        public string title { get; set; }
        public int publication_number { get; set; }
        public string publication_title { get; set; }
        public string isbn { get; set; }
        public string publisher { get; set; }
        public int citing_paper_count { get; set; }
        public IndexTerms index_terms { get; set; }
        public string pdf_url { get; set; }
        public string abstract_url { get; set; }
        public string html_url { get; set; }
        public Authors authors { get; set; }
        public string conference_location { get; set; }
        public string conference_dates { get; set; }
        public string start_page { get; set; }
        public string end_page { get; set; }
        public string @abstract { get; set; }
        public string issue { get; set; }
        public int? is_number { get; set; }
        public string volume { get; set; }
        public string issn { get; set; }
        public string publication_date { get; set; }
    }

    public class RootObject
    {
        public int total_records { get; set; }
        public int total_searched { get; set; }
        public List<Article> articles { get; set; }
    }

然后我尝试解析我的请求的reponse.Content对应于上面的JSON。

var client = new RestClient("http://ieeexploreapi.ieee.org/api/v1/search/articles?");
            var request = new RestRequest("articles", Method.GET);
            request.AddParameter("article_number", "6482896");
            request.AddParameter("format", "Json");
            request.AddParameter("max_records", "10");
            request.AddParameter("apikey", "somekey");
            var response = client.Execute(request);
            RestSharp.Deserializers.JsonDeserializer deserial = new JsonDeserializer();

            return new JsonResult()
            {
                Data = deserial.Deserialize<Article>(response);,
            };


            }

但是当我执行我的代码时,我遇到以下错误:类型为'IEEEAPI.Controllers.MinerController + Article',其中包含数据契约'MinerController.Article:http://schemas.datacontract.org/2004/07/IEEEAPI.Controllers'的名称。

有人可以解释我的问题吗?

0 个答案:

没有答案