Home > other >  How to deserialize json body with "0" in parameter
How to deserialize json body with "0" in parameter

Time:11-01

I'm using a API that give me this json as a response, This is json response body :

{
   "meta":{
      "status":200,
      "message":"success"
   },
   "data":{
      "0":{
         "MsgID":"2661689817",
         "Status":"6",
         "SendTime":"2021-10-3114:30:04",
         "DeliverTime":"2021-10-31 14:30:07"
      }
   }
}

My problem is "0":{... in this body.

How can I deserialize this into a Class.

it can't deserialize on "string _0" prop.

CodePudding user response:

As the @TheGeneral says, this is like a dictionary. You can parse - like this:

public void ParseObject()
{
    var response = @"{
                       'meta':{
                          'status':200,
                          'message':'success'
                       },
                       'data':{
                          '0':{
                             'MsgID':'2661689817',
                             'Status':'6',
                             'SendTime':'2021-10-3114:30:04',
                             'DeliverTime':'2021-10-31 14:30:07'
                          }
                       }
                    }";

    var responseObj = JsonConvert.DeserializeObject<MetaData>(response);
}

public class MetaData
{
    public Meta Meta { get; set; }
    public Dictionary<int, Data> Data { get; set; }
}

public class Meta
{
    public string Status { get; set; }
    public string Message { get; set; }
}

public class Data
{
    public string MsgId { get; set; }
    public string Status { get; set; }
    public string SendTime { get; set; }
}

CodePudding user response:

try using JsonProperty

public class Data
{
    [JsonProperty("0") ]
    public _0 _0 { get; set; }
}

public class _0
{
    public string MsgID { get; set; }
    public string Status { get; set; }
    public string SendTime { get; set; }
    public string DeliverTime { get; set; }
}
  • Related