Reputation: 39
I'm consuming a service that returns this JSON, when mapping in the model only takes me the first object but the sms
field with the other objects is empty.
This is the JSON:
{
"cliente": "999999",
"lote_id": "99999999999999",
"fecha_recepcion": "2019-04-29 12:31:35",
"resultado": 0,
"resultado_t": null,
"sms_procesados": 2,
"referecia": "Referencia Prueba",
"ip": "999.999.99.9",
"sms": {
"1": {
"id": "9999999",
"numero": "999999999",
"sms": "tests",
"fecha_envio": "2019-04-29 12:31:35",
"ind_area_nom": "cell",
"precio_sms": "9.00000",
"resultado_t": "",
"resultado": "0"
},
"2": {
"id": "8888888",
"numero": "9999998888",
"sms": "test",
"fecha_envio": "2019-04-29 12:31:35",
"ind_area_nom": "Celular",
"precio_sms": "9.00000",
"resultado_t": "",
"resultado": "0"
}
}
}
and this is my model:
public class ResultadoSms
{
public string cliente { get; set; }
public Int64 lote_id { get; set; }
public string fecha_recepcion { get; set; }
public Int64 resultado { get; set; }
public object resultado_t { get; set; }
public Int64 sms_procesados { get; set; }
public string referecia { get; set; }
public string ip { get; set; }
public Sms sms { get; set; }
}
public class Sms
{
public CuerpoSms CuerpoSms { get; set; }
}
public class CuerpoSms
{
public string id { get; set; }
public string numero { get; set; }
public string sms { get; set; }
public string fecha_envio { get; set; }
public string ind_area_nom { get; set; }
public string precio_sms { get; set; }
public string resultado_t { get; set; }
public string resultado { get; set; }
}
I tried to convert the sms
field into a list but it's still empty. I don't understand what the problem is or how I could deserialize the object in another easier way.
Upvotes: 0
Views: 45
Reputation: 129787
Your model is not accounting for the 1
, 2
, etc. keys inside the sms
object in the JSON. You need to use a Dictionary<string, CuerpoSms>
to handle that.
Change this line:
public Sms sms { get; set; }
To this:
public Dictionary<string, CuerpoSms> sms { get; set; }
Fiddle: https://dotnetfiddle.net/1XiwSF
Upvotes: 2
Reputation: 100607
The sms
in the returned JSON is an object with 2 properties, both having the same structure as your CuerpoSms
.
To have them automatically serialized in your C# code, you would need to:
public class Sms
{
[JsonProperty("1")]
public CuerpoSms CuerpoSms1 { get; set; }
[JsonProperty("2")]
public CuerpoSms CuerpoSms2 { get; set; }
}
Upvotes: 1