1
I’m starting to learn c# and I’m picking up to do this task, take an array of objects from a json and turn the elements of that array into objects of a specific type and put them in a list. Create a method that does this and returns a List of an object:
private List<Caneta> LerJsonCanetas()
{
string sJson = null;
JObject jObject = null;
List<Caneta> arrCanetas = null;
arrCanetas = new List<Caneta>();
//lê o json
sJson = File.ReadAllText(sArqConfiguracao); //sArqConfiguracao é o caminho para o arquivo json
jObject = JObject.Parse(sJson);
JArray sServer = (JArray)jObject["canetas"];
IList<Caneta> server = jObject["canetas"].ToObject<Caneta[]>();
foreach (Caneta caneta in server)
{
arrCaneta.Add(caneta);
}
return arrCaneta;
}
This is the method I created, it is not returning error, but the objects that are in the list are with null attributes, I wanted to know how to do so that I can get the objects that are in the json array and put in pen-like objects. I’m two days looking for how to do it and not get it yet. Can anyone help me? Thank you. Ah, this is my json:
{
"tempoexibicao": 10,
"canetas":
[
{
"cor" : "azul",
"marca" : "bic"
},
{
"cor" : "vermelha",
"marca" : "pilot"
}
]
}