I have a JSON string that I want to deserialize in an C# object
The JSON is a array of objects with five identical objects without names.
When I try to deserialize with the command:
JsonConvert.DeserializeObject<List<Model.AuxilioEmergencial>>(restResponse.Content);
I get a list with the five object but all of them null: Print of Visual Studio Debugger
take a look in JSON:
[
{
"id": 1,
"mesDisponibilizacao": "06/2020",
"beneficiario":
{
"nis": "123",
"nome": "JONI MITCHELL",
"multiploCadastro": false,
"cpfFormatado": "***.862.400-**"
},
"responsavelAuxilioEmergencial":
{
"nis": "123",
"nome": "JONI MITCHELL",
"nomeSemAcento": "JONI MITCHELL",
"cpfFormatado": "***.862.400-**"
},
"municipio":
{
"codigoIBGE": "123",
"nomeIBGE": "LAST TIME I SAW RICHARD",
"nomeIBGEsemAcento": "LAST TIME I SAW RICHARD",
"pais": "BRASIL",
"uf":
{
"sigla": "RS",
"nome": "RIO GRANDE DO SUL"
}
},
"valor": 10
},
{
"id": 2,
"mesDisponibilizacao": "06/2020",
"beneficiario":
{
"nis": "123",
"nome": "JONI MITCHELL",
"multiploCadastro": false,
"cpfFormatado": "***.862.400-**"
},
"responsavelAuxilioEmergencial":
{
"nis": "123",
"nome": "JONI MITCHELL",
"nomeSemAcento": "JONI MITCHELL",
"cpfFormatado": "***.862.400-**"
},
"municipio":
{
"codigoIBGE": "123",
"nomeIBGE": "LAST TIME I SAW RICHARD",
"nomeIBGEsemAcento": "LAST TIME I SAW RICHARD",
"pais": "BRASIL",
"uf":
{
"sigla": "RS",
"nome": "RIO GRANDE DO SUL"
}
},
"valor": 10
},
{
"id": 3,
"mesDisponibilizacao": "06/2020",
"beneficiario":
{
"nis": "123",
"nome": "JONI MITCHELL",
"multiploCadastro": false,
"cpfFormatado": "***.862.400-**"
},
"responsavelAuxilioEmergencial":
{
"nis": "123",
"nome": "JONI MITCHELL",
"nomeSemAcento": "JONI MITCHELL",
"cpfFormatado": "***.862.400-**"
},
"municipio":
{
"codigoIBGE": "123",
"nomeIBGE": "LAST TIME I SAW RICHARD",
"nomeIBGEsemAcento": "LAST TIME I SAW RICHARD",
"pais": "BRASIL",
"uf":
{
"sigla": "RS",
"nome": "RIO GRANDE DO SUL"
}
},
"valor": 40
},
{
"id": 4,
"mesDisponibilizacao": "06/2020",
"beneficiario":
{
"nis": "123",
"nome": "JONI MITCHELL",
"multiploCadastro": false,
"cpfFormatado": "***.862.400-**"
},
"responsavelAuxilioEmergencial":
{
"nis": "123",
"nome": "JONI MITCHELL",
"nomeSemAcento": "JONI MITCHELL",
"cpfFormatado": "***.862.400-**"
},
"municipio":
{
"codigoIBGE": "123",
"nomeIBGE": "LAST TIME I SAW RICHARD",
"nomeIBGEsemAcento": "LAST TIME I SAW RICHARD",
"pais": "BRASIL",
"uf":
{
"sigla": "RS",
"nome": "RIO GRANDE DO SUL"
}
},
"valor": 40
},
{
"id": 5,
"mesDisponibilizacao": "06/2020",
"beneficiario":
{
"nis": "123",
"nome": "JONI MITCHELL",
"multiploCadastro": false,
"cpfFormatado": "***.862.400-**"
},
"responsavelAuxilioEmergencial":
{
"nis": "123",
"nome": "JONI MITCHELL",
"nomeSemAcento": "JONI MITCHELL",
"cpfFormatado": "***.862.400-**"
},
"municipio":
{
"codigoIBGE": "123",
"nomeIBGE": "LAST TIME I SAW RICHARD",
"nomeIBGEsemAcento": "LAST TIME I SAW RICHARD",
"pais": "BRASIL",
"uf":
{
"sigla": "RS",
"nome": "RIO GRANDE DO SUL"
}
},
"valor": 50
}
]
and My C# class
[public class AuxilioEmergencial
{
public Auxilio\[\] auxilio { get; set; }
}
public class Auxilio
{
public int id { get; set; }
public string mesDisponibilizacao { get; set; }
public Beneficiario beneficiario { get; set; }
public Responsavelauxilioemergencial responsavelAuxilioEmergencial { get; set; }
public DadosMunicipio municipio { get; set; }
public float valor { get; set; }
}
public class Beneficiario
{
public string nis { get; set; }
public string nome { get; set; }
public bool multiploCadastro { get; set; }
public string cpfFormatado { get; set; }
}
public class Responsavelauxilioemergencial
{
public string nis { get; set; }
public string nome { get; set; }
public string nomeSemAcento { get; set; }
public string cpfFormatado { get; set; }
}
public class DadosMunicipio
{
public string codigoIBGE { get; set; }
public string nomeIBGE { get; set; }
public string nomeIBGEsemAcento { get; set; }
public string pais { get; set; }
public UF uf { get; set; }
}
public class UF
{
public string sigla { get; set; }
public string nome { get; set; }
}]
When you do
JsonConvert.DeserializeObject<List<Model.AuxilioEmergencial>>(restResponse.Content);
You're telling the deserializer that you want a List
, where every element is of type AuxilioEmergencial
.
Your class AuxilioEmergencial
has 1 property auxilio
, which means every object in the JSON array has to have that property to be properly deserialized. Eg:
[
{
"auxilio": {
"id ": 1,
"mesDisponibilizacao": "06/2020",
...
}
},
{
"auxilio": {
...
}
}
]
What you actually want is:
JsonConvert.DeserializeObject<List<Model.Auxilio>>(restResponse.Content);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.