无法反序列化 Json 文件虽然得到了响应

Posted

技术标签:

【中文标题】无法反序列化 Json 文件虽然得到了响应【英文标题】:unable to deserialization Jason file though got the results upto response 【发布时间】:2021-11-08 16:26:02 【问题描述】:

杰森文件 =

 
    "educations": 
        "count": 23,
        "education": [
                    "studId": "",
                    "joinDate": "2021-04-08 12:22",
                    "regDate": "2021-04-08 12:23",
                    "enrolled": false,
                    "eduId": "1-eassasa7",
                    "pId": "dfdgdfg456fghf",
                    "startDate": "2021-09-03",
                    "expiry": 
                        "monthlyExpire": true
                    ,
                    "reRegistration": "OPTIONAL",
                    "refreeId1": "",
                    "refreeId2": "",
                    "tutor": true,
                    "libraryAllowed": true,
                    "sports": "ENABLE",
                    "sendNotification": true,
                    "ccEmail": "",
                    "isTest": false,
                    "SportsGroup"

上面是我的杰森,我能够在回复中得到它

string rawResponse = apiDataResponse.Content;
var result = JsonConvert.DeserializeObject<RootData>(rawResponse);

以下是我的课程

  RootData
    
        [JsonProperty(PropertyName ="educations")]
        public education[] edu1  get; set;  
    

class educationss

    public int count  get; set;     
    [JsonProperty(PropertyName="education")]
    public education[] edu  get; set;     



class education
             
        public string studId  get; set;       
        public DateTime joinDate  get; set; 
        public DateTime regDate  get; set; 
        public bool enrolled  get; set; 
        public string eduId  get; set; 
        public string pId  get; set; 
        public DateTime startDate  get; set; 
        public expiry[] monthlyExpire  get; set; 
        public string reRegistration  get; set; 
        public string refreeId1  get; set; 
        public string refreeId2  get; set; 
        public bool tutor  get; set; 
        public bool LibraryAllowed  get; set; 
        public string sports  get; set; 
        public bool sendNotification  get; set; 
        public string ccEmail  get; set; 
        public bool isTest  get; set; 
        public sportGroup[] sportGroup  get; set; 
    

但我无法为我的结果变量获取任何值,它显示为 null。

Can you please help me?

【问题讨论】:

能否请您提供一个有效的 json? 【参考方案1】:

试试这个

var json= ...json string
var jsonDeserialized = JsonConvert.DeserializeObject<DataRoot>(json); 

jsonDeserialized.educations.education.ForEach(e =>

  Console.WriteLine($"joinDate: e.joinDate.ToString(),   pid: e.pId");
);

public class DataRoot

    public Educations educations  get; set; 



public class Educations

    public int count  get; set; 
    public List<Education> education  get; set; 

public class Education

    public string studId  get; set; 
    public string joinDate  get; set; 
    public string regDate  get; set; 
    public bool enrolled  get; set; 
    public string eduId  get; set; 
    public string pId  get; set; 
    public string startDate  get; set; 
    public Expiry expiry  get; set; 
    public string reRegistration  get; set; 
    public string refreeId1  get; set; 
    public string refreeId2  get; set; 
    public bool tutor  get; set; 
    public bool libraryAllowed  get; set; 
    public string sports  get; set; 
    public bool sendNotification  get; set; 
    public string ccEmail  get; set; 
    public bool isTest  get; set; 
    public object SportsGroup  get; set; 


public class Expiry

    public bool monthlyExpire  get; set; 

你必须修复你的 json


  "educations": 
    "count": 23,
    "education": [
      
        "studId": "",
        "joinDate": "2021-04-08 12:22",
        "regDate": "2021-04-08 12:23",
        "enrolled": false,
        "eduId": "1-eassasa7",
        "pId": "dfdgdfg456fghf",
        "startDate": "2021-09-03",
        "expiry": 
          "monthlyExpire": true
        ,
        "reRegistration": "OPTIONAL",
        "refreeId1": "",
        "refreeId2": "",
        "tutor": true,
        "libraryAllowed": true,
        "sports": "ENABLE",
        "sendNotification": true,
        "ccEmail": "",
        "isTest": false,
        "SportsGroup": null
      
    ]
  

【讨论】:

以上是关于无法反序列化 Json 文件虽然得到了响应的主要内容,如果未能解决你的问题,请参考以下文章

将Json字符串反序列化为对象java

无法反序列化 JSON 响应

反序列化以 @ 开头的 JSON 属性

得到“JSON 请求太大而无法反序列化”

WCF 无法反序列化 JSON 请求

反序列化问题