0

Currently i have this json:

{
    "ResponseCode":200,
     "data":[
       "2016-08-05T15:49:15.157000+00:00",
       "SomeString",
        1230,
        9.025
    ]
}

I can deserialize json string to this

public class Example1
{
    public int ResponseCode { get; set; }
    public IList<object> data { get; set; }
}

But i would like to deserialize “data” to a concrete model instead of IList<object>

public class Example1
{
    public int ResponseCode { get; set; }

    public DataModel data { get; set; }
}

public class DataModel
{

    public DateTime date { get; set; }

    public string SomeString { get; set; }

       ...

}

Im using System.Text.Json nuget package. What should i do? Can i solve this with a custom implementation of JsonConvert<T>?

dbc
  • 104,963
  • 20
  • 228
  • 340
SzAdam88
  • 3
  • 1
  • You will need a custom serialization converter, or write the handling of it manually, because the json denotes a list of various types of objects, and there isn't something built in to do that. – Lasse V. Karlsen Apr 29 '20 at 11:09

1 Answers1

0

If you want to use DataModel class, you need to update your json value :

{
    "ResponseCode":200,
     "data": {
       "date": "2016-08-05T15:49:15.157000+00:00",
       "someString": "SomeString",
       "anotherVal": 1230,
       "anotherVal2": 9.025
    }
}

where data is Example1.data parameter and "date", "someString", "anotherVal" and "anotherVal2" are DataModel parameters

AlleXyS
  • 2,476
  • 2
  • 17
  • 37
  • Is there any solution to do that which can work in performance critical environement and its “dynamic”? – SzAdam88 Apr 29 '20 at 12:04