[ Deserialize multiple row with DataContractJsonSerializer class ]
I've an API that return this:
{
"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
}
}
I'm trying to parse it whit DataContractJsonSerializer class... i've maked support class in this way:
[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; }
}
If I use List it doesn't work... and in this way, parsing is very slow... is there any solution? thanks in advance
Answer 1
In your response object EXAMS_LIST should be defined as Dictionary<int, Exam>
. The problem is that DataContractJsonSerializer can not handle dictionaries properly. Your best bet is to use JSON.NET from Newtonsoft. Here is how you'd do it with JSON.NET:
Change EXAMS_LIST to 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; }
}
Call JSON.NET
StudyPlanResponse response = JsonConvert.DeserializeObject<StudyPlanResponse>(File.ReadAllText("TextFile1.json"));
Answer 2
The source uses a format that is compatible with a Dictionary
rather than the more common List
or Array
, so the short answer is to use a Dictionary
to deserialize the EXAMS_LIST
element.
Two answers, both using the following structure:
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; }
}
}
First, using JSON.Net
:
StudyPlanResponse studyplan = JsonConvert.DeserializeObject<StudyPlanResponse>(jsrc);
Second, using 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);
}
I prefer the JSON.Net version, personally. The DataContractJsonSerializer
version works, once you tell it to use the simple dictionary format via the settings object.