This is the JSON string which I received
{
"Date":"2021-11-16",
"Name":"Raj",
"BError":{
"code":"errorcode",
"details":"message"
},
"AStatus":true
}
I have to Deserialize the above JSON string
I have given the class details with JSON annotations below
public class Demo
{
[JsonProperty("Date")]
public DateTime? Date{ get; set; }
pulic string Name{get;set;}
[JsonProperty("B-Error")]
public BError BError{ get; set; }
[JsonProperty("A-Status")]
public bool AStatus { get; set; }
}
public class BError
{
public string code { get; set; }
public string details { get; set; }
}
the code I have written to Deserialize is
var responseJson = JsonConvert.DeserializeObject(input_JSON_string).ToString();
Demo d = JsonConvert.DeserializeObject<Demo>(responseJson);
this code is converting input_JSON_string into object but not all fields. The fields "Date" and "Name" is converting but the fields "B-Error" and "A-Status" is storing values as NULL.
How to Deserialize all the fields?
This is supposed to be the name of the property in the json
[JsonProperty("B-Error")]
"B-Error" is not the name of the property in the json; your json property is called "BError"
To get a full set of working classes together with hints about how to deserialise, just paste your json into https://QuickType.io
you can add a constructor if you want it the way you want
public partial class Demo
{
[JsonProperty("Date")]
public DateTimeOffset Date { get; set; }
[JsonProperty("Name")]
public string Name { get; set; }
[JsonProperty("B-Error")]
public BError BError { get; set; }
[JsonProperty("A-Status")]
public bool AStatus { get; set; }
[JsonConstructor]
public Demo(DateTimeOffset Date,string Name, BError BError, bool AStatus)
{
this.Date=Date;
this.Name=Name;
this.BError=BError;
this.AStatus=AStatus;
}
}
public partial class BError
{
[JsonProperty("code")]
public string Code { get; set; }
[JsonProperty("details")]
public string Details { get; set; }
}
this json was tested and code is working properly
{
"Date":"2021-11-16",
"Name":"Raj",
"BError":{
"code":"errorcode",
"details":"message"
},
"AStatus":true
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.