c#无法反序列化JSON对象

时间:2018-11-08 15:43:16

标签: c# json json.net

我只想返回Guideline列表对象,但是TResult将仅接受字符串响应对象。

  

我应该使用TResult返回对象吗?

错误消息: 无法将类型'System.Collections.Generic.List'隐式转换为'TResult'

程序类

var internalResp = DSB.GetResource<List<DashboardServices.Dto.DiseaseDto.Guideline>>(url);

ServiceBase类

 public TResult GetResource<TResult>(string url)
        {

            DiseaseDto.RootObject objGuideline = null;
            using (var webClient = new WebClient())
            {
                var response = Encoding.UTF8.GetString(webClient.DownloadData(url));


                objGuideline = JsonConvert.DeserializeObject<DiseaseDto.RootObject>(response);


                return JsonConvert.DeserializeObject<TResult>( response);


            }
        }

DTO

 public class RootObject
        {
            [JsonProperty("Login")]
            public Login Login { get; set; }
            [JsonProperty("Guidelines")]
            public List<Guideline> Guidelines { get; set; }
        }

enter image description here

1 个答案:

答案 0 :(得分:0)

这是解决问题的方法。我认为您不太了解泛型的工作原理。您不能反序列化为具体类型,然后将其作为泛型传递回去。您必须告诉方法要反序列化的内容(泛型类型),然后将其返回。因此,仅需要在您的方法之外进行“准则”的提取。

GetResource方法:

public static T GetResource<T>(string url)
{
  var response = Encoding.UTF8.GetString(webClient.DownloadData(url));
  T obj = JsonConvert.DeserializeObject<T>(response);
  return obj;
}

调用它的代码:

List<DashboardServices.Dto.DiseaseDto.Guideline> internalResp = DSB.GetResource<DashboardServices.DtoDiseaseDto.RootObject>(url).Guidelines;

DTO:

public class DiseaseDto
{
    public class Login
    {
        public bool IsValid
        {
            get;
            set;
        }

        public string Message
        {
            get;
            set;
        }
    }

    public class ListOfDiseas
    {
        public int DiseaseId
        {
            get;
            set;
        }

        public string Name
        {
            get;
            set;
        }

        public object DiseaseList
        {
            get;
            set;
        }

        public object GuidelineDiseaseList
        {
            get;
            set;
        }

        public bool IsReadOnly
        {
            get;
            set;
        }

        public object ListOfDiseaseTypes
        {
            get;
            set;
        }
    }

    public class Guideline
    {
        public int GuidelineId
        {
            get;
            set;
        }

        public string Name
        {
            get;
            set;
        }

        public string Type
        {
            get;
            set;
        }

        public List<ListOfDiseas> ListOfDiseases
        {
            get;
            set;
        }

        public string Url
        {
            get;
            set;
        }

        public string LatestVersionNumber
        {
            get;
            set;
        }

        public DateTime LastPublishedDate
        {
            get;
            set;
        }

        public string Language
        {
            get;
            set;
        }

        public string PublishedForRegion
        {
            get;
            set;
        }
    }

    public class RootObject
    {
        public Login Login
        {
            get;
            set;
        }

        public List<Guideline> Guidelines
        {
            get;
            set;
        }
    }

这是一个使用静态JSON的精简工作演示,但演示了原理:https://dotnetfiddle.net/JN17u8