Question

I am having issues processing a JSON in the following format,which doesn't specify a root element.

{
  "name":"Test"
  "age":"15"
  "gender":"Male"

}

My class to represent looks like the following.

public class Person{

 public string name { get; set; }
 public string age { get; set; }
 public string gender { get; set; }

}

Any my web method which processes is like below.

[System.Web.Services.WebMethod]
public boolean deserializePerson(Person person)
{
   //implementation code
}

This web service method fails to deserialize the JSON, as it expects a JSON in the following format with the root element(class name in this case) specified.

{
   Person:{
      "name":"Test"
      "age":"15"
      "gender":"Male"
     }
}

Is there any way to deserialize the JSON without needing to specify the root element Person? Perhaps via annotations?

Thanks.

No correct solution

OTHER TIPS

if you have this class on both side it it will work perfectly

public class Person
{

    public string name { get; set; }
    public string age { get; set; }
    public string gender { get; set; }

    public string getClassSerialized()
    {
        return new JavaScriptSerializer().Serialize(this);
    }

    public Person getClassDeserialized(string sSerializedClass)
    {
        return new JavaScriptSerializer().Deserialize<Person>(sSerializedClass);
    }
}

to get the class back you may get it like this :

var myPerson = new Person().getClassDeserialized(sSerializedClass);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top