我有一个具有以下属性的类: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字符串定义的类 From是另一个.cs文件
public string id { get; set; }
public string name { 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)
由于 Smitha
答案 0 :(得分:1)
你需要在彼此内部有两个foreach
个循环;每个List<>
级别一个。
答案 1 :(得分:0)
@Slaks解决方案应该适合你。虽然我认为您的数据更好地表示为List<Education>
(或者更好,IEnumerable<Education>
),您可能想要做的就是将其展平。
最好在源头压扁它,以确保您的代码在其他地方更清洁。
如果您使用的是.NET 3.5,则可以像
那样进行操作var flattenData = data.SelectMany(x => x);
如果您使用的是.NET 3.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;
}