0

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);
}
  • That's because the root element has a `Data` property where the rest of the properties are. Add `public class Root { public Data Data { get; set; } }` and then deserialize into that instead. – Lasse V. Karlsen Apr 08 '21 at 10:56
  • Didnt get ur point – user3064181 Apr 08 '21 at 10:58
  • Does this answer your question? [How to auto-generate a C# class file from a JSON string](https://stackoverflow.com/questions/21611674/how-to-auto-generate-a-c-sharp-class-file-from-a-json-string) – Drag and Drop Apr 08 '21 at 11:30

2 Answers2

1

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; }
}
Steve Harris
  • 5,014
  • 1
  • 10
  • 25
1

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?

Fildor
  • 14,510
  • 4
  • 35
  • 67
  • I did try that way and changed the class for deserializing. But i am geting below error. – user3064181 Apr 08 '21 at 11:16
  • Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'ConsoleApplication1.Data' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. – user3064181 Apr 08 '21 at 11:16
  • I'll look into that. Please stand by. – Fildor Apr 08 '21 at 11:17
  • 1
    @user3064181 Perhaps you mistyped the code, it works fine. Make sure the `Data` property is an array as shown above. – DavidG Apr 08 '21 at 11:18
  • 1
    Yes its working fine now thanks for the help – user3064181 Apr 08 '21 at 11:20
  • Worked for me, too: https://dotnetfiddle.net/49aBG4 – Fildor Apr 08 '21 at 11:20