Link to home
Start Free TrialLog in
Avatar of flynny
flynnyFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Parsing returned List<KeyValuePair<String,String>> javascript from AJAX call

HI,

I return a List<KeyValuePair<String,String>> form a C# webmethod ajax call.

    runAjax('/Public/AdminServices.asmx/GetFonts', null)
    .done(function (fonts) {
        alert(fonts.d); //alerts Object object Object object

        var html = "<ul class='fontList'>";

        for(var i=0; i<fonts.d.length; i++)
        {
            //both alerting undefined??
            alert(fonts.d[i][0].toString() + fonts.d[i][1].toString());
            html += "<li data-url='" + fonts.d[i][0] + "'>" + fonts.d[i][1] + "</li>";
        }

Open in new window


my webmethod works as follows ( and form debugging is returning 2 lines of values);

HttpWebRequest request = WebRequest.Create(fonts_path) as HttpWebRequest;
        request.Accept = "*/*";
        WebResponse response = request.GetResponse();
        Regex regex = new Regex("(?i)<a href=\"(?<url>.*?)\">(?<text>.*?)</a>");
        List<KeyValuePair<String, String>> files = new List<KeyValuePair<String, String>>();

        using (var reader = new StreamReader(response.GetResponseStream()))
        {
            string result = reader.ReadToEnd();
            MatchCollection matches = regex.Matches(result);

            if (matches.Count == 0)
            {
                Global.log.Info("ERROR: Trying to list fonts from resource directory");
                return null;
            }

            foreach (Match match in matches)
            {
                if (!match.Success) { continue; }

                if (match.Groups["url"].ToString() != "/")
                {
                    Global.log.Info("Debug: MATCH :" + match.Groups["url"].ToString() + ":" +  match.Groups["text"].ToString() + ":");
                    files.Add(new KeyValuePair<String, String>(match.Groups["url"].ToString(),match.Groups["text"].ToString()));
                }
            }
        }

        return files;

Open in new window


any ideas how I can get the two values from the list on the clientside return?
ASKER CERTIFIED SOLUTION
Avatar of mccarl
mccarl
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of flynny

ASKER

thanks as it was off topic from the previous question i opened a new one. Sorry i didnt realised you replied there!
You're welcome!

No worries. I think that I posted in the other question after you had opened this question (which is probably the right thing to do anyway). I just hadn't seen this new question before I posted the reply in the other one.

Oh, BTW, another syntax you could use is.... fonts.d[ i].Key and fonts.d[ i].Value    The .dot notation or the array [subscript] notation should work the same.