-1

In the picture you can see what is in ht, but how do i parse the values from ht..

hashtable

its a php serialization that i deserialize with this function:

this is the post source:

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
    }

And then I get this in ht

I have searched and used google, but haven't found an answer.

m00p
  • 93
  • 1
  • 1
  • 6
  • Are you asking because you don't know the key values ahead of time, or are what you posted the key values of every entry? – Ron Beyer Apr 28 '15 at 12:24
  • 3
    Please don't post your code / your data as a screenshot. – germi Apr 28 '15 at 12:25
  • Are you asking about `item.Key` and `item.Value` properties? Btw use `Dictionary` instead of `Hashtable`. It is typesafe. – Sriram Sakthivel Apr 28 '15 at 12:26
  • And use a generic type-safe `List` instead of `ArrayList`. – dcastro Apr 28 '15 at 12:32
  • What type is `serializer`? It looks like you might have a list of dynamic or JSon objects but the ArrayList is from an earlier era. Have a look at the Raw View. – H H Apr 28 '15 at 12:33
  • If you don't have a class with Id and Code etc already then the idea in the now deleted and downvoted answer is correct: write a class to represent the results. – H H Apr 28 '15 at 12:36
  • What parsing result are you expecting? It's not clear, what do you mean by "parse". Why are you using `ArrayList` and `HashTable` at all? According to your edits: could you post source string here? Maybe, data comes in standard format like JSON, and you're re-inventing the wheel. – Dennis Apr 28 '15 at 12:48
  • check edits for post source – m00p Apr 28 '15 at 13:05
  • @BjornR: this is PHP serialization format. Look here: http://stackoverflow.com/questions/1914585/how-to-unserialize-php-serialized-array-variable-class-and-return-suitable-objec – Dennis Apr 28 '15 at 13:38

1 Answers1

-1

The following answer should be flagged with "maybe" as it is really hard to see whats behind your given code.

Usually a DictionaryEntry provides a structure with Key and Value. But in your case this isn't even needed as your ArrayList contains just "Sub-Lists". So if I understand you correctly it would be enough to write:

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

By writing foreach(DictionaryEntry in ht) you suppose that ht is a Dictionary. But in fact it is a List which contains Dictionarys

I hope this solves your problem (If I understood it correctly)

Jibbow
  • 757
  • 1
  • 8
  • 17
  • 1
    The problem with `ArrayList` is that it contains `object`s. So `item["id"]` isn't going to compile because `object` doesn't have an indexer property. The OP needs to cast `item` to whatever is inside of the `ArrayList`. Maybe a `HashTable`? – juharr Apr 28 '15 at 12:48
  • Juharr is right just item["id"] doesn't work – m00p Apr 28 '15 at 13:00
  • @Jibbow Given the code the OP has shown it's not obvious if they can switch to using generics, but if they can then they definitely should. – juharr Apr 28 '15 at 13:03
  • @Jibbow I get an error InvalidCastException: Can not convert an object of type System.Collections.Hashtable the type System.Collections.Generic.Dictionary`2 [ System.String , System.String ] – m00p Apr 28 '15 at 13:21
  • OMG its working, Thanks so much :p , you have no idea how long I have been looking for this problem!! I guess I was searching in the wrong direction – m00p Apr 28 '15 at 13:29
  • @Jibbow: congratulations. You've just helped to re-invented the wheel. – Dennis Apr 28 '15 at 13:39
  • @Dennis I know that this is not a perfect solution and there are a lot of small improvements to be done. But hey, he's a beginner and I think is this appropriate for the work he wants to do. – Jibbow Apr 28 '15 at 13:45