Saturday, 15 May 2010

c# - Deserialize JSON formatted data -



c# - Deserialize JSON formatted data -

i have json info string, retrieved online, through webrequest. trying parse string , map info onto info structure, not working...i have used same method other mappings well, worked far. in particular case, not working..

this json formatted string retrieved:

{"item":{"firstname":"antônio","lastname":"da silva","commonname":null,"height":"175","dateofbirth":{"year":"1978","month":"6","day":"13"},"preferredfoot":"left","clubid":"1825","leagueid":"20","nationid":"54","rating":"70","attribute1":"53","attribute2":"68","attribute3":"74","attribute4":"73","attribute5":"55","attribute6":"56","rare":"1","itemtype":"playerm"}}

this class parses string:

public class parseplayerinfo { private playerclass playerinfo; public playerclass parse(string stringtoparse) { javascriptserializer serializer = new javascriptserializer(); if ((stringtoparse != null) && (stringtoparse.length > 0)) { this.playerinfo = serializer.deserialize<playerclass>(stringtoparse); } homecoming this.playerinfo; } }

this player class:

public class playerclass { public playerclass() { this.player = new playercontents(); } public playercontents player { get; set; } }

this class of playercontent:

public class playercontents { public playercontents() { string str; this.itemtype = str = ""; this.rare = str = str; this.attribute6 = str = str; this.attribute5 = str = str; this.attribute4 = str = str; this.attribute3 = str = str; this.attribute2 = str = str; this.attribute1 = str = str; this.rating = str = str; this.nationid = str = str; this.leagueid = str = str; this.clubid = str = str; this.preferredfoot = str = str; this.height = str = str; this.commonname = str = str; this.firstname = this.lastname = str; this.dateofbirth = new dob(); } public string attribute1 { get; set; } public string attribute2 { get; set; } public string attribute3 { get; set; } public string attribute4 { get; set; } public string attribute5 { get; set; } public string attribute6 { get; set; } public string clubid { get; set; } public string commonname { get; set; } public dob dateofbirth { get; set; } public string firstname { get; set; } public string height { get; set; } public string itemtype { get; set; } public string lastname { get; set; } public string leagueid { get; set; } public string nationid { get; set; } public string preferredfoot { get; set; } public string rare { get; set; } public string rating { get; set; } }

edit:

this dob class:

public class dob { public dob() { this.year = this.month = this.day; } public string day { get; set; } public string month { get; set; } public string year { get; set; } }

any ideas?

try renaming player property match property in json, i.e item

c# json serialization

No comments:

Post a Comment