I have a simple serialized json array
string json = "[{\"id\":100,\"UserId\":99},{\"id\":101,\"UserId\":98}]";
var data = (List<Model>)Newtonsoft.Json.JsonConvert.DeserializeObject(json , typeof(List<Model>));
my model to deserialize:
public class Model
{
public int? id { get; set; }
public int? UserId { get; set; }
}
What is the best way to retrieve data from each Index[?] and print it to Console ?
2 Answers 2
You can do a foreach
loop:
foreach(var item in data) {
Console.WriteLine(item.UserId);
}
answered Mar 22, 2016 at 14:31
Comments
string json = "[{\"id\":100,\"UserId\":99},{\"id\":101,\"UserId\":98}]";
var objects = JArray.Parse(json);
var firstIndexValue = objects[0];
Console.WriteLine(firstIndexValue);
foreach (var index in objects)
{
Console.WriteLine(index);
}
for (int index = 0; index < objects.Count; index++)
{
Console.WriteLine(objects[index]);
}
answered Mar 22, 2016 at 14:25
1 Comment
APALALA
thanks for the help . But what if i don't know the index value instead i want to print each index value one by one ?
lang-cs
data[n]
anddata.Count
, exactly? Also you can get rid of the cast by usingvar data = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Model>>(json);
instead.