我有一个简单的序列化json数组
string json = "[{"id":100,"UserId":99},{"id":101,"UserId":98}]";
var data = (List<Model>)Newtonsoft.Json.JsonConvert.DeserializeObject(json , typeof(List<Model>));
我的反序列化模型:
public class Model
{
public int? id { get; set; }
public int? UserId { get; set; }
}
从每个索引[?]中检索数据并将其打印到控制台的最佳方式是什么?
您可以执行foreach
循环:
foreach(var item in data) {
Console.WriteLine(item.UserId);
}
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]);
}