1

我有一个 Object 类型的变量,它包含这样的数据

项目[0]=[ {id=a,name=b,url=c},{id=d,name=e,url=f},{id=a,name=b,url=c}... ]

项目[1]=[ {id=a,name=b,url=c},{id=d,name=e,url=f},{id=a,name=b,url=c}... ]

......

我怎么能循环这个?

更新

            if (null != myProjects) //myProjectsis of type object
            {
                dirEntry = myProjects as Dictionary<string, object>;
                dirDetails = dirEntry["Response"] as Dictionary<string, object>;
                object projects = null;
                foreach (var item in dirDetails)
                {
                    if ("Items" == item.Key)
                    {
                        projects = dirDetails["Items"];
                        break;
                    }
                }
              

现在projects 将包含对象数组。我不想循环它来获取一些值

4

1 回答 1

3

你可以按照这个样本,

项目数据类(id、name、url)

public class ObjectList
{
    public ObjectList(string id1, string name1, string url1)
    {
        id = id1;
        name = name1;
        url = url1;
    }
    public string id { get; set; }
    public string name { get; set; }
    public string url { get; set; }
}

数组项(项目)的类

 public class RootObj
    {
        public string objectType { get; set; }
        public List<ObjectList> objectList { get; set; }
    }

获得基于对象的数组后,您可以按照以下示例循环浏览项目。

private void ManipulateProjects()
{
    object[] projects = new object[5];

    RootObj item1Obj = new RootObj();
    List<ObjectList> item1List = new List<ObjectList>();
    item1List.Add(new ObjectList("1", "Rone1", "htt://google1.com"));
    item1List.Add(new ObjectList("2", "Rone2", "htt://google2.com"));
    item1List.Add(new ObjectList("3", "Rone3", "htt://google3.com"));
    item1Obj.objectList = item1List;
    projects[0] = item1Obj;

    RootObj item2Obj = new RootObj();
    List<ObjectList> item2List = new List<ObjectList>();
    item2List.Add(new ObjectList("10", "Rone10", "htt://google10.com"));
    item2List.Add(new ObjectList("12", "Rone12", "htt://google20.com"));
    item2List.Add(new ObjectList("13", "Rone13", "htt://google30.com"));
    item2Obj.objectList = item2List;
    projects[1] = item2Obj;

    //Once you have your formatted array of projects then it is just a matter of looping through it.
    foreach (RootObj item in projects)
    {
        if (item == null) continue;
        List<ObjectList> items = item.objectList;

        foreach (ObjectList item1 in items)
        {
            //Response.Write(item1.id + " " + item1.name + " " + item1.url + "<br />");
        }
    }
}
于 2013-10-31T07:38:38.103 回答