I'm starting to learn C # and am getting to do this task, grabbing an array of objects from a json and transform 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 do I get the objects that are in the json array and put them in objects type pen. I'm two days away looking for how to do it and can not do it yet. Can someone help me? Thanks. Oh, this is my json:
{
"tempoexibicao": 10,
"canetas":
[
{
"cor" : "azul",
"marca" : "bic"
},
{
"cor" : "vermelha",
"marca" : "pilot"
}
]
}