【问题标题】:How to parse Hashtable / ArrayList [duplicate]如何解析 Hashtable / ArrayList [重复]
【发布时间】:2015-07-07 07:12:44
【问题描述】:

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

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

这是帖子来源:

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

中得到这个

我已经搜索并使用了谷歌,但没有找到答案。

【问题讨论】:

  • 你问是因为你不提前知道关键值,还是你发布了每个条目的关键值?
  • 请不要将您的代码/您的数据作为屏幕截图发布。
  • 您询问的是item.Keyitem.Value 属性吗?顺便说一句,使用Dictionary&lt;TKey,TValue&gt; 而不是Hashtable。它是类型安全的。
  • 并使用通用类型安全的List&lt;T&gt; 而不是ArrayList
  • serializer 是什么类型?看起来您可能有一个动态或 JSon 对象列表,但 ArrayList 来自更早的时代。看看原始视图。

标签: c# serialization arraylist deserialization hashtable


【解决方案1】:

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

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

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

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

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

【讨论】:

  • ArrayList 的问题在于它包含objects。所以item["id"] 不会编译,因为object 没有索引器属性。 OP 需要将item 转换为ArrayList 内部的任何内容。也许是HashTable
  • Juharr 是对的,只是 item["id"] 不起作用
  • @Jibbow 鉴于 OP 显示的代码,他们是否可以切换到使用泛型并不明显,但如果可以,那么他们肯定应该这样做。
  • @Jibbow 我收到错误 InvalidCastException:无法将 System.Collections.Hashtable 类型的对象转换为 System.Collections.Generic.Dictionary`2 [ System.String , System.String ]跨度>
  • 天哪,它的工作,非常感谢:p,你不知道我找这个问题多久了!!我想我找错方向了
猜你喜欢
  • 1970-01-01
  • 2015-12-28
  • 1970-01-01
  • 2016-08-23
  • 2015-03-01
  • 1970-01-01
  • 2011-02-24
  • 2016-10-27
  • 2013-05-09
相关资源
最近更新 更多