C#列表<;列表<;T>&燃气轮机;
我有一个具有以下属性的类:Education.cs 为可能是最基本的问题道歉C#列表<;列表<;T>&燃气轮机;,c#,asp.net,C#,Asp.net,我有一个具有以下属性的类:Education.cs 为可能是最基本的问题道歉 public List<List<Education>>data { get; set; } public class Education { public From school { get; set; } public From year { get; set; } public string type { get; set; } } 这是我的json字符串 ed
public List<List<Education>>data { get; set; }
public class Education
{
public From school { get; set; }
public From year { get; set; }
public string type { get; set; }
}
这是我的json字符串
education": [
{
"school": {
"id": "107751089258341",
"name": "JNTU, Kakinada, India"
},
"year": {
"id": "132393000129123",
"name": "2001"
},
"type": "College"
},
{
"school": {
"id": "103710319677602",
"name": "University of Houston"
},
"year": {
"id": "113125125403208",
"name": "2004"
},
"type": "Graduate School"
}
]
有人能告诉我如何访问教育成员(学校、年级)?这对你来说可能是小菜一碟。
在我的aspx.cs中,
我必须写一个foreach或任何其他文件来访问我的变量school.name、year.name
必须将该类成员的访问权限写入我的aspx.cs
url= "https://graph.facebook.com/me?fields=education&access_token=" + oAuth.Token;
json = oAuth.WebRequest(oAuthFacebook.Method.GET, url, String.Empty);
List<Education>??? = js.Deserialize<List<??>(json)
url=”https://graph.facebook.com/me?fields=education&access_token=“+oAuth.Token;
json=oAuth.WebRequest(oAuthFacebook.Method.GET,url,String.Empty);
列表???=反序列化您需要两个foreach
循环,每个循环都在内部;每个级别的列表都有一个@Slaks解决方案应该适合您。虽然我相信您的数据更好地表示为List
(或者更好地表示为IEnumerable
),但您可能需要做的是将其展平
最好是在源代码处将其展平,以确保您的代码在其他地方更干净
如果你在.NET3.5上,你可以像这样做
var flattenData = data.SelectMany(x => x);
如果您使用的是.NET3.5/C#3.0之前的版本,您可以这样做
//Your original code block
{
IEnumerable<Education> flattenData = FlattenMyList(data);
//use flatten data normally
}
IEnumerable<T> FlattenMyList<T> (List<List<T> data){
foreach(List<T> innerList in data){
foreach(T item in innerList){
yield return item;
}
}
yield break;
}
//原始代码块
{
IEnumerable FlattyData=FlattyList(数据);
//正常使用展平数据
}
IEnumerable压扁列表(ListAccess什么?foreach循环?看起来不像完整的代码?你能完成它吗?你有嵌套列表的原因吗?确切地说,
中的是什么类型?你能告诉我们你到底想实现什么吗?这似乎有点奇怪。@Smitha,看看我的答案。同样,@Slaks解决方案应该适合你。
//Your original code block
{
IEnumerable<Education> flattenData = FlattenMyList(data);
//use flatten data normally
}
IEnumerable<T> FlattenMyList<T> (List<List<T> data){
foreach(List<T> innerList in data){
foreach(T item in innerList){
yield return item;
}
}
yield break;
}