I am getting JSON in string format from a URL whose structure is like this, but I am unable to parse it. It's throwing an exception, any idea how to parse it?
Here is the structure:
{
"pathway":{
"patients":{
"patient":[
{
"patientid":"7703176",
"name":"Abbot, Bud",
"status":"Invited",
"start":"2013-12-07",
"last":"N/A",
"engagement":"N/A",
"drug":"N/A",
"adherence":"N/A",
"vitals":"Current",
"last_appointment":"2013-10-25",
"next_appointment":"None"
},
{
"patientid":"5089554",
"name":"Brennan, Bonnie",
"status":"Connected",
"start":"2013-12-29",
"last":"2014-02-01",
"engagement":"Low",
"drug":" ",
"adherence":" ",
"vitals":"Out of Date",
"last_appointment":"2013-04-21",
"next_appointment":"None"
}
]
}
}
}
i am doing like this:
public class PathWayWrapper
{
public pathway pathway { get; set; }
}
and
public class pathway
{
public List<patient> patients { get; set; }
}
and
public class patient
{
public long patientid { get; set; }
public string name { get; set; }
public string status { get; set; }
public string start { get; set; }
public string last { get; set; }
public string engagement { get; set; }
public string drug { get; set; }
public string adherence { get; set; }
public string vitals { get; set; }
public string last_appointment { get; set; }
public string next_appointment { get; set; }
}
here is my parsing code:
StreamReader reader = new StreamReader(response.GetResponseStream());
string json = reader.ReadToEnd();
var Jsonobject = JsonConvert.DeserializeObject<PathWayWrapper>(json);
objPathway = Jsonobject.pathway;
The classes into which you're deserializing are incorrect. You are missing the "Patients" class.
When I have JSON and am trying to deserialize it I like to use http://json2csharp.com to generate a first cut at the deserialization classes. It's more accurate than trying to do it by hand.
P.S. The exception you were getting is usually a really good clue that there's something wrong with the way you've defined the deserialization classes.