使用DataContractJsonSerializer类反序列化多行

时间:2014-06-23 23:31:12

标签: c# .net windows-phone-8

我有一个返回此的API:

{
"CODE": 200,
"EXAMS_LIST": {
    "0": {
        "CFU": " 9",
        "RESULT": "SOSTENUTO",
        "SSD": " MAT/05",
        "TAF": " A",
        "TEACHING": "MATEMATICA I CFU 9",
        "YEAR": 1
    },
    "1": {
        "CFU": " 4",
        "RESULT": "Sufficiente",
        "SSD": " L-LIN/12",
        "TAF": " E",
        "TEACHING": "LINGUA INGLESE (COLLOQUIO)",
        "YEAR": 1
    },
    "2": {
        "CFU": " 6",
        "RESULT": "SOSTENUTO",
        "SSD": " FIS/01",
        "TAF": " A",
        "TEACHING": "FISICA CFU 6",
        "YEAR": 1
    },
    ...........
    "22": {
        "CFU": " 5",
        "RESULT": " ",
        "SSD": "  ",
        "TAF": " E",
        "TEACHING": "PROVA FINALE CFU 5",
        "YEAR": 3
    }
}

我正在尝试使用DataContractJsonSerializer类解析它...我已经用这种方式制作了支持类:

[DataContract]
public class Exam
{
    [DataMember(Name = "CFU", IsRequired = true)]
    private string cfu;
    [DataMember(Name = "RESULT", IsRequired = true)]
    private string result;
    [DataMember(Name = "SSD")]
    private string ssd;
    [DataMember(Name = "TAF")]
    private string taf;
    [DataMember(Name = "TEACHING", IsRequired = true)]
    private string teaching;
    [DataMember(Name = "YEAR", IsRequired = true)]
    private string year;

    public string Cfu
    {
        get { return cfu; }
        set { cfu = value; }
    }
    .........
    public string Year
    {
        get { return year; }
        set { year = value; }
    }
}}


[DataContract]
public class StudyPlanResponse
{
    [DataMember(Name="CODE")]
    public string Code {get;set;}
    [DataMember(Name = "EXAMS_LIST")]
    public ExamList Exams { get; set; }


}

[DataContract]
public class ExamList
{

    [DataMember(Name = "0")]
    public Exam ex0 { get; set; }
    [DataMember(Name = "1")]
    public Exam ex1 { get; set; }
    ............
    [DataMember(Name = "30")]
    public Exam ex30 { get; set; }

}

如果我使用List它不起作用......这样,解析速度很慢......有什么解决方案吗?提前谢谢

2 个答案:

答案 0 :(得分:0)

在您的响应对象中,EXAMS_LIST应定义为Dictionary<int, Exam>。问题是DataContractJsonSerializer无法正确处理字典。最好的办法是使用Newtonsoft的JSON.NET。以下是您使用JSON.NET的方式:

将EXAMS_LIST更改为Dictionary<int, Exam>

[DataContract]
public class StudyPlanResponse
{
    [DataMember(Name = "CODE")]
    public string Code { get; set; }            
    [DataMember(Name = "EXAMS_LIST")]
    public Dictionary<int, Exam> Exams { get; set; }
}

调用JSON.NET

StudyPlanResponse response = JsonConvert.DeserializeObject<StudyPlanResponse>(File.ReadAllText("TextFile1.json"));

答案 1 :(得分:0)

源使用的格式与Dictionary兼容,而不是更常见的ListArray,因此简短的回答是使用Dictionary反序列化EXAMS_LIST元素。

两个答案,都使用以下结构:

public class StudyPlanResponse
{
    public int CODE { get; set; }
    public Dictionary<int, Exam> EXAMS_LIST { get; set; }

    public class Exam
    {
        public string CFU { get; set; }
        public string RESULT { get; set; }
        public string SSD { get; set; }
        public string TAF { get; set; }
        public string TEACHING { get; set; }
        public int YEAR { get; set; }
    }
}

首先,使用JSON.Net

StudyPlanResponse studyplan = JsonConvert.DeserializeObject<StudyPlanResponse>(jsrc);

其次,使用DataContractJsonSerializer

var settings = new DataContractJsonSerializerSettings();
settings.UseSimpleDictionaryFormat = true;

DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(StudyPlanResponse), settings);
StudyPlanResponse studyplan;
using (var stream = new System.IO.MemoryStream(Encoding.UTF8.GetBytes(jsrc)))
{
    studyplan = (StudyPlanResponse)ser.ReadObject(stream);
}

我个人更喜欢JSON.Net版本。一旦您通过设置对象告诉它使用简单字典格式,DataContractJsonSerializer版本就可以工作。