tutorials.de Buch-Aktion 05/2012
ERLEDIGT
JA
ANTWORTEN
2
ZUGRIFFE
192
EMPFEHLEN
  • An Twitter übertragen
  • An Facebook übertragen
AUF DIESES THEMA
ANTWORTEN
  1. #1
    Raven280438 Raven280438 ist offline Mitglied Brokat
    Registriert seit
    Aug 2007
    Beiträge
    329
    Hi,

    Ich lese aus einer Webseite den Quellcode aus. Der Quellcode bildet ein PHP-Array ab:

    Code :
    1
    2
    3
    
    $array["0"]["name"] = "Username";
    $array["0"]["pass"] = "Password";
    $array["0"]["path"] = "/path";

    Wie kann ich jetzt am einfachsten das PHP-Array in ein C# Dictionary parsen?


    Gruß
     

  2. #2
    Avatar von Nico Graichen
    Nico Graichen Nico Graichen ist offline aka gemballa
    tutorials.de Moderator
    Registriert seit
    Dec 2003
    Ort
    Pulheim (NRW)
    Beiträge
    3.898
    Blog-Einträge
    34
    Hi

    Über die String-Methoden den entsprechenden Werte suchen (Split, IndexOf, Substring seihen hier initial genannt).
    Da hier ein Schema drin ist kannst du auch RegularExpressions nutzen.
     
    Grüße Nico
    ----------------------
    Xing
    ----------------------
    Zitat Zitat von Mark Twain (1835-1910)
    Es gibt drei Dinge, die eine Frau aus dem Nichts hervorzaubern kann: einen Hut, einen Salat und einen Ehekrach.
    Zitat Zitat von Mike Wilson - Biographie über Larry Ellison (CEO Oracle)
    The Difference Between God and Larry Ellison: God Doesn't Think He's Larry Ellison

  3. #3
    Raven280438 Raven280438 ist offline Mitglied Brokat
    Registriert seit
    Aug 2007
    Beiträge
    329
    Hi,

    ich habs jetzt mit nem RegEx gemacht.
    Hier der Code falls jemand das gleiche Problem hat:

    Code :
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    
                    WebClient wClient = new WebClient();
                    wClient.Encoding = Encoding.UTF8;
                    String strSource = wClient.DownloadString(url);
     
                    String[] Lines = strSource.Split('\n');
     
                    List<Dictionary<String, String>> ListReturn = new List<Dictionary<string,string>>();
     
                    System.Text.RegularExpressions.Regex regExpr = new System.Text.RegularExpressions.Regex("^\\$array\\[\"([0-9].*)\"\\]\\[\"([0-9a-zA-Z_\\-].*)\"\\] = \"(.*)\";$");
     
                    foreach (String Line in Lines)
                    {
                        if (regExpr.IsMatch(Line))
                        {
                            System.Text.RegularExpressions.Match match = regExpr.Match(Line);
     
                            System.Text.RegularExpressions.Group g_id = match.Groups[1];
                            System.Text.RegularExpressions.Group g_key = match.Groups[2];
                            System.Text.RegularExpressions.Group g_value = match.Groups[3];
                            System.Text.RegularExpressions.CaptureCollection cc_id = g_id.Captures;
                            System.Text.RegularExpressions.Capture c_id = cc_id[0];
                            System.Text.RegularExpressions.CaptureCollection cc_key = g_key.Captures;
                            System.Text.RegularExpressions.Capture c_key = cc_key[0];
                            System.Text.RegularExpressions.CaptureCollection cc_value = g_value.Captures;
                            System.Text.RegularExpressions.Capture c_value = cc_value[0];
     
                            int id = Convert.ToInt16(c_id.ToString());
                            String key = c_key.ToString();
                            String value = c_value.ToString();
     
                            if (ListReturn.Count() > id && ListReturn[id] != null)
                            {
                                ListReturn[id].Add(key, value);
                            }
                            else
                            {
                                Dictionary<String, String> directory = new Dictionary<string,string>();
                                directory.Add(key, value);
                                ListReturn.Add(directory);
                            }
                        }
                    }
     

Ähnliche Themen

  1. Antworten: 3
    Letzter Beitrag: 04.10.10, 18:46
  2. C# Dictionary
    Von Alexander_87 im Forum .NET Windows Forms
    Antworten: 1
    Letzter Beitrag: 02.12.09, 16:15
  3. Antworten: 14
    Letzter Beitrag: 06.05.08, 18:23
  4. imap Datei parsen und array Problem
    Von palman im Forum PHP
    Antworten: 0
    Letzter Beitrag: 05.09.07, 15:00
  5. String in Array parsen
    Von Jens B. im Forum PHP
    Antworten: 11
    Letzter Beitrag: 31.05.06, 17:31