C# List<List<T>>
I have a class with the following property: Educ开发者_运维百科ation.cs Apologize for the perhaps basic question
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; }
}
This is the class I have defined for deserializing json string From is another .cs file
public string id { get; set; }
public string name { get; set; }
Here is my json string
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"
}
]
Can someone tell me how to access the members of Education(school, year)? It could be a piece of cake for you. In my aspx.cs, I have to write a foreach or any other to access my variables, school.name, year.name Will have to work this access of class members into my 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)
Thanks Smitha
You need two foreach
loops inside of eachother; one for each level of List<>
.
@Slaks solution should work for you. Though I believe your data is better represented as List<Education>
(or better still, IEnumerable<Education>
) and what you may want to do is to Flatten it out.
It would be better to flatten it out at the source, to make sure your code is cleaner at other places.
If you are on .NET 3.5 you can do it like
var flattenData = data.SelectMany(x => x);
If you are on pre .NET 3.5 / C# 3.0, you can do it like this
//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;
}
精彩评论