如何解析 Hashtable / ArrayList

How to parse Hashtable / ArrayList

在图片中您可以看到 ht 中的内容,但是我如何解析 ht..

中的值

它是我用这个函数反序列化的 php 序列化:

这是 post 来源:

a:47:{i:0;a:4:{s:2:"id";s:4:"1789";s:4:"code";s:3:"1AA";s:4:"name";s:3:"1AA";s:4:"desc";s:15:"eerste Latijn a";}i:1;a:4:{s:2:"id";s:4:"1791";s:4:"code";s:3:"1BA";s:4:"name";s:3:"1BA";s:4:"desc";s:15:"eerste Latijn b";}i:2;a:4:{s:2:"id";s:4:"1795";s:4:"code";s:3:"1CA";s:4:"name";s:3:"1CA";s:4:"desc";s:40:"eerste moderne wetenschappen optie talen";}i:3;a:4:{s:2:"id";s:4:"1793";s:4:"code";s:3:"1DA";s:4:"name";s:3:"1DA";s:4:"desc";s:40:"eerste moderne wetenschappen optie sport";}i:4;a:4:{s:2:"id";s:4:"1797";s:4:"code";s:3:"1EA";s:4:"name";s:3:"1EA";s:4:"desc";s:50:"eerste moderne wetenschapen optie M.O., P.O., T.O.";}i:5;a:4:{s:2:"id";s:4:"1799";s:4:"code";s:3:"1FA";s:4:"name";s:3:"1FA";s:4:"desc";s:38:"eerste moderne wetenschapen optie taal";}i:6;a:4:{s:2:"id";s:4:"1801";s:4:"code";s:3:"2LA";s:4:"name";s:3:"2LA";s:4:"desc";s:17:"tweedejaar Latijn";}i:7;a:4:{s:2:"id";s:4:"1803";s:4:"code";s:4:"2MOA";s:4:"name";s:4:"2MOA";s:4:"desc";s:45:"tweede jaar moderne wetenschappen optie talen";}i:8;a:4:{s:2:"id";s:4:"1805";s:4:"code";s:4:"2MOB";s:4:"name";s:4:"2MOB";s:4:"desc";s:45:"tweede jaar moderne wetenschappen optie sport";}i:9;a:4:{s:2:"id";s:4:"1807";s:4:"code";s:4:"2MOC";s:4:"name";s:4:"2MOC";s:4:"desc";s:53:"tweede jaar moderne wetenschappen optie mens en media";}i:10;a:4:{s:2:"id";s:4:"1809";s:4:"code";s:4:"2MOD";s:4:"name";s:4:"2MOD";s:4:"desc";s:45:"tweede jaar moderne wetenschappen optie talen";}i:11;a:4:{s:2:"id";s:4:"1606";s:4:"code";s:3:"3EC";s:4:"name";s:3:"3EC";s:4:"desc";s:10:"3 Economie";}i:12;a:4:{s:2:"id";s:4:"1516";s:4:"code";s:3:"3HW";s:4:"name";s:3:"3HW";s:4:"desc";s:22:"3 Humane Wetenschappen";}i:13;a:4:{s:2:"id";s:4:"1536";s:4:"code";s:3:"3LT";s:4:"name";s:3:"3LT";s:4:"desc";s:24:"3 Latijn - Moderne Talen";}i:14;a:4:{s:2:"id";s:4:"1538";s:4:"code";s:3:"3LW";s:4:"name";s:3:"3LW";s:4:"desc";s:19:"3 Latijn - Wiskunde";}i:15;a:4:{s:2:"id";s:4:"1688";s:4:"code";s:3:"3SW";s:4:"name";s:3:"3SW";s:4:"desc";s:20:"3 Sportwetenschappen";}i:16;a:4:{s:2:"id";s:4:"1787";s:4:"code";s:3:"3WE";s:4:"name";s:3:"3WE";s:4:"desc";s:15:"3 Wetenschappen";}i:17;a:4:{s:2:"id";s:4:"1518";s:4:"code";s:3:"4EC";s:4:"name";s:3:"4EC";s:4:"desc";s:10:"4 Economie";}i:18;a:4:{s:2:"id";s:4:"1535";s:4:"code";s:3:"4HW";s:4:"name";s:3:"4HW";s:4:"desc";s:22:"4 Humane Wetenschappen";}i:19;a:4:{s:2:"id";s:4:"1646";s:4:"code";s:3:"4LT";s:4:"name";s:3:"4LT";s:4:"desc";s:24:"4 Latijn - Moderne Talen";}i:20;a:4:{s:2:"id";s:4:"1523";s:4:"code";s:3:"4LW";s:4:"name";s:3:"4LW";s:4:"desc";s:19:"4 Latijn - Wiskunde";}i:21;a:4:{s:2:"id";s:4:"1692";s:4:"code";s:3:"4SW";s:4:"name";s:3:"4SW";s:4:"desc";s:20:"4 Sportwetenschappen";}i:22;a:4:{s:2:"id";s:4:"1682";s:4:"code";s:3:"4WE";s:4:"name";s:3:"4WE";s:4:"desc";s:15:"4 Wetenschappen";}i:23;a:4:{s:2:"id";s:4:"1765";s:4:"code";s:4:"5EMT";s:4:"name";s:4:"5EMT";s:4:"desc";s:26:"5 Economie - Moderne Talen";}i:24;a:4:{s:2:"id";s:4:"1656";s:4:"code";s:4:"5EWE";s:4:"name";s:4:"5EWE";s:4:"desc";s:26:"5 Economie - Wetenschappen";}i:25;a:4:{s:2:"id";s:4:"1611";s:4:"code";s:4:"5EWI";s:4:"name";s:4:"5EWI";s:4:"desc";s:21:"5 Economie - Wiskunde";}i:26;a:4:{s:2:"id";s:4:"1654";s:4:"code";s:4:"5GLA";s:4:"name";s:4:"5GLA";s:4:"desc";s:17:"5 Grieks - Latijn";}i:27;a:4:{s:2:"id";s:4:"1526";s:4:"code";s:4:"5HWE";s:4:"name";s:4:"5HWE";s:4:"desc";s:22:"5 Humane Wetenschappen";}i:28;a:4:{s:2:"id";s:4:"1540";s:4:"code";s:4:"5LMT";s:4:"name";s:4:"5LMT";s:4:"desc";s:24:"5 Latijn - Moderne Talen";}i:29;a:4:{s:2:"id";s:4:"1548";s:4:"code";s:4:"5LWE";s:4:"name";s:4:"5LWE";s:4:"desc";s:24:"5 Latijn - Wetenschappen";}i:30;a:4:{s:2:"id";s:4:"1694";s:4:"code";s:4:"5LWI";s:4:"name";s:4:"5LWI";s:4:"desc";s:19:"5 Latijn - Wiskunde";}i:31;a:4:{s:2:"id";s:4:"1684";s:4:"code";s:4:"5SWE";s:4:"name";s:4:"5SWE";s:4:"desc";s:20:"5 Sportwetenschappen";}i:32;a:4:{s:2:"id";s:4:"1680";s:4:"code";s:6:"5SWEST";s:4:"name";s:6:"5SWEST";s:4:"desc";s:26:"5 Sportwetenschappen Stene";}i:33;a:4:{s:2:"id";s:4:"1531";s:4:"code";s:4:"5TWE";s:4:"name";s:4:"5TWE";s:4:"desc";s:31:"5 Moderne Talen - Wetenschappen";}i:34;a:4:{s:2:"id";s:4:"1528";s:4:"code";s:4:"5WWI";s:4:"name";s:4:"5WWI";s:4:"desc";s:26:"5 Wetenschappen - Wiskunde";}i:35;a:4:{s:2:"id";s:4:"1785";s:4:"code";s:4:"6EMT";s:4:"name";s:4:"6EMT";s:4:"desc";s:26:"6 Economie - Moderne Talen";}i:36;a:4:{s:2:"id";s:4:"1721";s:4:"code";s:4:"6EWE";s:4:"name";s:4:"6EWE";s:4:"desc";s:26:"6 Economie - Wetenschappen";}i:37;a:4:{s:2:"id";s:4:"1650";s:4:"code";s:4:"6EWI";s:4:"name";s:4:"6EWI";s:4:"desc";s:21:"6 Economie - Wiskunde";}i:38;a:4:{s:2:"id";s:4:"1698";s:4:"code";s:4:"6GLA";s:4:"name";s:4:"6GLA";s:4:"desc";s:17:"6 Grieks - Latijn";}i:39;a:4:{s:2:"id";s:4:"1524";s:4:"code";s:4:"6HWE";s:4:"name";s:4:"6HWE";s:4:"desc";s:22:"6 Humane Wetenschappen";}i:40;a:4:{s:2:"id";s:4:"1546";s:4:"code";s:4:"6LMT";s:4:"name";s:4:"6LMT";s:4:"desc";s:24:"6 Latijn - Moderne Talen";}i:41;a:4:{s:2:"id";s:4:"1527";s:4:"code";s:4:"6LWE";s:4:"name";s:4:"6LWE";s:4:"desc";s:24:"6 Latijn - Wetenschappen";}i:42;a:4:{s:2:"id";s:4:"1549";s:4:"code";s:4:"6LWI";s:4:"name";s:4:"6LWI";s:4:"desc";s:19:"6 Latijn - Wiskunde";}i:43;a:4:{s:2:"id";s:4:"1690";s:4:"code";s:4:"6SWE";s:4:"name";s:4:"6SWE";s:4:"desc";s:20:"6 Sportwetenschappen";}i:44;a:4:{s:2:"id";s:4:"1686";s:4:"code";s:6:"6SWEST";s:4:"name";s:6:"6SWEST";s:4:"desc";s:26:"6 Sportwetenschappen Stene";}i:45;a:4:{s:2:"id";s:4:"1543";s:4:"code";s:4:"6TWE";s:4:"name";s:4:"6TWE";s:4:"desc";s:31:"6 Moderne Talen - Wetenschappen";}i:46;a:4:{s:2:"id";s:4:"1529";s:4:"code";s:4:"6WWI";s:4:"name";s:4:"6WWI";s:4:"desc";s:26:"6 Wetenschappen - Wiskunde";}}



private object deserialize(string str)
    {
        if (str == null || str.Length <= this.pos)
            return new Object();

        int start, end, length;
        string stLen;
        switch (str[this.pos])
        {
            case 'N':
                this.pos += 2;
                return null;
            case 'b':
                char chBool;
                chBool = str[pos + 2];
                this.pos += 4;
                return chBool == '1';
            case 'i':
                string stInt;
                start = str.IndexOf(":", this.pos) + 1;
                end = str.IndexOf(";", start);
                stInt = str.Substring(start, end - start);
                this.pos += 3 + stInt.Length;
                object oRet = null;
                try
                {
                    //firt try to parse as int
                    oRet = Int32.Parse(stInt, this.nfi);
                }
                catch
                {
                    //if it failed, maybe it was too large, parse as long
                    oRet = Int64.Parse(stInt, this.nfi);
                }
                return oRet;
            case 'd':
                string stDouble;
                start = str.IndexOf(":", this.pos) + 1;
                end = str.IndexOf(";", start);
                stDouble = str.Substring(start, end - start);
                this.pos += 3 + stDouble.Length;
                return Double.Parse(stDouble, this.nfi);
            case 's':
                start = str.IndexOf(":", this.pos) + 1;
                end = str.IndexOf(":", start);
                stLen = str.Substring(start, end - start);
                int bytelen = Int32.Parse(stLen);
                length = bytelen;
                //This is the byte length, not the character length - so we might  
                //need to shorten it before usage. This also implies bounds checking
                if ((end + 2 + length) >= str.Length) length = str.Length - 2 - end;
                string stRet = str.Substring(end + 2, length);
                while (this.StringEncoding.GetByteCount(stRet) > bytelen)
                {
                    length--;
                    stRet = str.Substring(end + 2, length);
                }
                this.pos += 6 + stLen.Length + length;
                if (this.XMLSafe)
                {
                    stRet = stRet.Replace("\n", "\r\n");
                }
                return stRet;
            case 'a':
                //if keys are ints 0 through N, returns an ArrayList, else returns Hashtable
                start = str.IndexOf(":", this.pos) + 1;
                end = str.IndexOf(":", start);
                stLen = str.Substring(start, end - start);
                length = Int32.Parse(stLen);
                Hashtable htRet = new Hashtable(length);
                ArrayList alRet = new ArrayList(length);
                this.pos += 4 + stLen.Length; //a:Len:{
                for (int i = 0; i < length; i++)
                {
                    //read key
                    object key = deserialize(str);
                    //read value
                    object val = deserialize(str);

                    if (alRet != null)
                    {
                        if (key is int && (int)key == alRet.Count)
                            alRet.Add(val);
                        else
                            alRet = null;
                    }
                    htRet[key] = val;
                }
                this.pos++; //skip the }
                if (this.pos < str.Length && str[this.pos] == ';')//skipping our old extra array semi-colon bug (er... php's weirdness)
                    this.pos++;
                if (alRet != null)
                    return alRet;
                else
                    return htRet;
            default:
                return "";
        }//switch
    }

然后我在 ht

中得到这个

我搜索并使用了google,但没有找到答案。

下面的答案应该用 "maybe" 标记,因为很难看出给定代码背后的内容。

通常一个DictionaryEntry提供一个结构KeyValue。但在你的情况下,这甚至不需要,因为你的 ArrayList 只包含 "Sub-Lists"。 所以如果我理解正确的话,这样写就足够了:

foreach(Hashtable item in ht)    
{
    Console.WriteLine(item["id"]);
}

通过写作 foreach(DictionaryEntry in ht) 你假设 htDictionary。但实际上它是一个包含 Dictionarys

List

我希望这能解决你的问题(如果我理解正确的话)