[英]How to Add list Item to a class Object
我从API调用中获得了Task(List(object))的返回值,并且试图将此列表添加到定义为Student的类对象中,但未将任何数据添加到我的类对象中
我返回的列表将是这样的
[
{
"Name": "Something no 1",
"Age": 20
},
{
"Name": "Something no 2",
"Age": 21
},
{
"Name": "Something no 3",
"Age": 11
}
]
我的课:
public class Students
{
public string Name;
public int Age;
}
public ObservableCollection<Students> students = new ObservableCollection<Students>();
List<object> response_list = await response;
foreach ( var item1 in response_list)
{
if (item1 != null)
{
students.Add(item1 as Students);
}
}
现在,我尝试了另一种方式:
由于我已经编写了一个类,因此可以使用NewtonSoft.json直接将其反序列化为我的类对象,但是它也不起作用。
public ObservableCollection<Students> students = new ObservableCollection<Students>();
//public int Max_width;
string responseString = await response;
students = JsonConvert.DeserializeObject<ObservableCollection<Students>>(responseString);
但这也失败了
尝试这个:
foreach ( dynamic item1 in response_list)
{
if (item1 != null)
{
discussions_cnt_new.Add(new Students() { Name = item1.Name, Age = item1.Age });
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.