So i send an Json object to my server, and i want to convert it to a strongly typed user type object that i have called PersonalPreferences.
Here is the JS
{
"PersonalPreferences": [
{
"FavoriteFood": "Pasta with butter and cheese"
},
{
"FavoriteSport": "Submission Wrestling"
},
{
"FavoriteGame": "Starcraft 2"
},
{
"FavoriteMusic": "Hip Hop"
}
]
}
I have created a Class that matches the Json object i am sending.
[DataContract]
public class PersonalPreferences
{
[DataMember]
public string FavoriteFood { get; set; }
[DataMember]
public string FavoriteSport { get; set; }
[DataMember]
public string FavoriteGame { get; set; }
[DataMember]
public string FavoriteMusic { get; set; }
public PersonalPreferences()
{
}
public PersonalPreferences(string favoriteFood, string favoriteGame, string favoriteMusic, string favoriteSport)
{
this.FavoriteFood = favoriteFood;
this.FavoriteGame = favoriteGame;
this.FavoriteMusic = FavoriteMusic;
this.FavoriteSport = favoriteSport;
}
}
And here is my handler that receives the Request and coverts it to the user type object.
My problem is that the PersonalPreference object is empty. The jsSerializer.Deserialize calls the empty constructor and the values from the Json object is not passed to the PersonalPreference object.
I have checked that the Json value exists in the Request as a string.
[WebService(Namespace = "http://localhost:53243")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class JSonTestHandler : IHttpHandler, System.Web.SessionState.IReadOnlySessionState
{
public void ProcessRequest(HttpContext context)
{
string jsonData = new StreamReader(context.Request.InputStream, System.Text.Encoding.UTF8).ReadToEnd();
var jsSerializer = new JavaScriptSerializer();
var personalPreferences = jsSerializer.Deserialize(jsonData, typeof(PersonalPreferences));
}}
It seems some thing wrong with supplied json formatted string.
if Check with following json formatted string it’s works fine:
How did you generated the Json formatted string?