I have a JSON data string, which I retrieved online, through a webrequest. I am trying to parse the string and map the data onto my data structure, which is not working…I have used the same method previously for other mappings as well, which worked well so far. But in this particular case, it is not working..
This is my JSON formatted string that is 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 is my class which parses the 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);
}
return this.playerInfo;
}
}
This is my Player class:
public class PlayerClass
{
public PlayerClass()
{
this.Player = new PlayerContents();
}
public PlayerContents Player { get; set; }
}
This is my 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 is my 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 your
Playerproperty to match the property in JSON, i.eItem