I want my json string data to be converted to an object.
Here is my json data
{
"first_name": "Abdullah",
"last_name": "Khan"
}
Here is my C# code:
string data = System.IO.File.ReadAllText(Server.MapPath(@"~/TextFiles/test.json"));
Test test = Newtonsoft.Json.JsonConvert.DeserializeObject<Test>(data);
Below is my getter setter class
public class Test
{
private string first_name { get; set; }
private string last_name { get; set; }
}
I have tried using the DeserializeObject() method but it does not put the value in the properties.
CodePudding user response:
declare the properties public like this
public class Test
{
public string first_name { get; set; }
public string last_name { get; set; }
}
also one small suggestion, please give the class a meaningful name like Student
or Employee
instead of Test.
CodePudding user response:
or just for a record, it will work with Newtonsoft.Json too, but it doesn't make much sense if your class doesn't have any another code
public class Test
{
[JsonProperty("first_name")]
private string first_name { get; set; }
[JsonProperty("last_name")]
private string last_name { get; set; }
}