0

I have a JSON object returned from API with the below format:

{
    "responseStatus": "SUCCESS",
    "responseDetails": {
        "limit": 1000,
        "offset": 0,
        "size": 2,
        "total": 2
    },
    "data": [
        {
            "tbl.col": "data"
        },
        {
            "tbl.col": "data"
        }
    ]
}

I need to map the above JSON object "data" to a class or to generic type list

Stefan Popovski
  • 486
  • 6
  • 22
  • please...1) search for an existing answer, 2) post what you have tried. https://stackoverflow.com/questions/58364283/map-json-object-to-c-sharp-class-property-array – smoore4 Feb 02 '20 at 17:03
  • Please provide any sample code not only your JSON result – Stefan Popovski Feb 02 '20 at 17:05
  • If you're woriied about the dot in `tbl.col` see [this answer](https://stackoverflow.com/questions/36376524/accessing-properties-with-a-dot-in-their-name/36377225#36377225) – Arthur Rey Feb 02 '20 at 17:24

1 Answers1

0

The way to do it is like this...

First your model has to be like this:

    public class ResponseModel
    {
      public string responseStatus { get; set; }
      public ResponseDetail responseDetails { get; set; }

      public List<DataModel> data = new List<DataModel>();
    }
    public class ResponseDetail
    {
      public int limit { get; set; }
      public int offset { get; set; }
      public int size { get; set; }
      public int total { get; set; }
    }

    public class DataModel
    {
      [JsonProperty(PropertyName = "tbl.col")]
      public string tbl_col { get; set; }
    }

Then you need to call:

var response = JsonConvert.DeserializeObject<ResponseModel>(inputJSONStringGoesHere);

You will need to add Newtonsoft JSON nuget package for this to work.

Also the attribute

[JsonProperty(PropertyName = "tbl.col")]

is used in the sample code because C# does not allow "." (dots) in member names.

Jonathan Alfaro
  • 4,013
  • 3
  • 29
  • 32