문제

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.

올바른 솔루션이 없습니다

다른 팁

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);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top