Easiest way to parse “querystring” formatted data

后端 未结 6 1854
误落风尘
误落风尘 2020-11-29 09:11

With the following code:

string q = \"userID=16555&gameID=60&score=4542.122&time=343114\";

What would be the easiest way to par

6条回答
  •  粉色の甜心
    2020-11-29 09:46

    How is this

    using System.Text.RegularExpressions;
    
    // query example
    //   "name1=value1&name2=value2&name3=value3"
    //   "?name1=value1&name2=value2&name3=value3"
    private Dictionary ParseQuery(string query)
    {
        var dic = new Dictionary();
        var reg = new Regex("(?:[?&]|^)([^&]+)=([^&]*)");
        var matches = reg.Matches(query);
        foreach (Match match in matches) {
            dic[match.Groups[1].Value] = Uri.UnescapeDataString(match.Groups[2].Value);
        }
        return dic;
    }
    

提交回复
热议问题