0

I need to map json having property name with dots to the equivalent nested c# classes.

For e.g. I have JSON string as below:

{
    "DateProcessed": "20170215T005736+0530", //e.g.to ProcessedDate in C# class
    "WordCount" :346,
    "Content.Headline": "This is just the headline",
    "Content.Href": "http://www.samplesssss.test",
    "Content.Text": "This is the big text represntation here that defines the summary structure.",
    "Content.Thumbnail.Href" : "http://imagetest.test.intr",
    "Content.Thumbnail.Height" : "245",
    "Content.Thumbnail.Width" :"345",
    "Content.Thumbnail.UnitType" :"centimeter"
}

I need to map the above JSON string to the C# class mentioned below:

public class Body
{
    public Datetime ProcessedDate {get;set;}
    public int WordCount {get;set;}
    public Format Content {get;set;}
}

public class Content
{
    public string Headline {get;set;}
    public string Href {get;set;}
    public string Text {get;set;}
    public ImageHolder Thumbnail {get;set;}
}

public class ImageHolder
{
    public string Href {get;set;}
    public int Height {get;set;}
    public int Width {get;set;}
    public string UnitType {get;set;}
}

Really clueless as how to map the above JSON with C# Classes. I tried with traditional approach by splitting string and trying to find equivalent matching property name in C# class but it became so messy I had to stop it and delete everything. Any approach will be really helpful. Hope SO expertise will save my day.

Joshua I
  • 550
  • 3
  • 8
  • 22
  • see this: https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-serialize-and-deserialize-json-data – Sparrow Dec 05 '17 at 21:54
  • Or this https://www.newtonsoft.com/json/help/html/SerializingJSON.htm – Sparrow Dec 05 '17 at 21:56
  • @Sparrow Unfortunately those are not at all helpful as those are mostly for straight forward serializung and deserializing that has proper JSON structure. – Joshua I Dec 05 '17 at 22:01
  • If the format of your json data is always like that, then you can create another class that can be mapped to this json (straight forward) and then map that class into these 3 classes. – Sparrow Dec 05 '17 at 22:18
  • @Sparrow then whats the point, I will end up having same issue again. If there is any solution based on the question asked I would appreciate the help. – Joshua I Dec 05 '17 at 22:32
  • 2
    Looks like a duplicate of [Mapping flat JSON/Dictionary to model (containing sub classes)](https://stackoverflow.com/q/33026829/3744182). – dbc Dec 05 '17 at 22:34
  • 1
    @dbc Thanks mate this is what i was looking for. I will try the solution and i will post if that works..Meanwhile thank you so much. – Joshua I Dec 05 '17 at 22:39
  • @dbc just one more thing how to map invidual field that is different, e.g. in the question JSON has DateProcessed and C# Class has ProcessedDate, Is there a way i can configure the map matching with different name.Thanks – Joshua I Dec 05 '17 at 22:55
  • @JoshuaI try using `[JsonProperty("DateProcessed")]` on your `ProcessedDate` property. – Brian Rogers Dec 05 '17 at 22:58
  • @Brian, it throws an error, Could not convert string to DateTime: 20170215T005736+0530. Path 'DateProcessed'.. I need to handle type conversion as well.. not sure where I need to plug that in. – Joshua I Dec 05 '17 at 23:02
  • 2
    @JoshuaI That is a non-standard date format so you'll need a custom converter for that too. See [fiddle](https://dotnetfiddle.net/e80fqj). – Brian Rogers Dec 05 '17 at 23:42
  • 1
    Actually it looks like [ISO 8601 "Basic Format"](https://en.wikipedia.org/wiki/ISO_8601#General_principles) -- a format with no separators. If I put the separators in manually, then both `JsonConvert.DeserializeObject("\"2017-02-15T00:57:36+0530\"")` and `DateTime.Parse("2017-02-15T00:57:36+0530")` now work. Looks like the basic format is not supported automatically by Json.NET, so you will need to write your own converter as @BrianRogers wrote. – dbc Dec 05 '17 at 23:48
  • Thanks @Brian and dbc you did save my day. Yes I implemented my own datetime converter. The only problem this Json to C# class is bit slow. Not sure how to improve the performance. Is there no other way to improve performance. – Joshua I Dec 05 '17 at 23:52
  • 1
    @JoshuaI That is a whole other ball of wax. I suggest you start with [Performance Tips](https://www.newtonsoft.com/json/help/html/Performance.htm) in the documentation. Also try searching on google and StackOverflow. I'm sure this question has been asked many times before. You may have to abandon these nice converters and drop down to using a bare `JsonTextReader` instead if performance is that critical. Or use a different serializer altogether. Post a new question if you get stuck. – Brian Rogers Dec 06 '17 at 00:10
  • Thanks @Brian. I will try then. I really appreciate your help. – Joshua I Dec 06 '17 at 10:27

0 Answers0