Reputation: 3
I have an input json data which is serialized may i know how to deserialize as due my current code its coming as empty
Input Json:
"{\"Data\":[{\"Comments\":\"Please enter comments\",\"TimeStamp\":\"20210331031638.210\",\"UserName\":\"Rocky\"}]}\n"
Code:
public class DBData
{
[JsonProperty("Comments")]
public char Comments { get; set; }
[JsonProperty("TimeStamp")]
public char TimeStamp { get; set; }
[JsonProperty("UserName")]
public char UserName { get; set; }
}
void fun()
{
var outObject1 = JsonConvert.DeserializeObject<DBData>(jsonData);
}
Upvotes: 0
Views: 1342
Reputation: 5109
Your property types are 'char'. That is not right. Change them to string.
Also, I believe your JSON data has a hierarchy that may not be included in your deserialization so you need a class containing a property of your DBData class to deserialize into.
EDIT:
To make the JSON data easier to understand you could reformat it as follows (just for viewing purposes as there is nothing wrong with it for actual use):
{
"Data": [
{
"Comments": "Please enter comments",
"TimeStamp": "20210331031638.210",
"UserName": "Rocky"
}
]
}
I've removed the escape characters and anything else that's not important for this. You can see that the data property is an array (enclosed in square brackets []), so potentially can include any number of instances of the data.
So actually, this is the correct class model for your JSON (note the attributes are not required):
public class DBData
{
public Data[] Data { get; set; }
}
public class Data
{
public string Comments { get; set; }
public string TimeStamp { get; set; }
public string UserName { get; set; }
}
Upvotes: 1
Reputation: 16094
What @LasseV.Karlsen tried to layout in his comment:
Your Json suggests for the model to have this structure:
class DBData
{
public Data[] Data {get; set;} // Data is an array of data rows.
}
class Data
{
[JsonProperty("Comments")] // <- Mind that these are redundant.
public string Comments { get; set; } // <- Also mind, the props should be of type string
[JsonProperty("TimeStamp")]
public string TimeStamp { get; set; }
[JsonProperty("UserName")]
public string UserName { get; set; }
}
A char in C#/.Net is very different from a string. You may have been confused by some database type varchar or nvarchar, maybe?
Upvotes: 1