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

3
  • please...1) search for an existing answer, 2) post what you have tried. stackoverflow.com/questions/58364283/… Commented Feb 2, 2020 at 17:03
  • Please provide any sample code not only your JSON result Commented Feb 2, 2020 at 17:05
  • If you're woriied about the dot in tbl.col see this answer Commented Feb 2, 2020 at 17:24

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.