Web-url parameter Resolution

Source: Internet
Author: User

Requirements: To remove the URL, delete all parameters in the value content, only the path and key are preserved

Solution:

1. The path and Param are separated by the semicolon character (?) first

2. Then parse the Param, use & to Split, get each Itema

3. Finally, use = for Itema to split, retain key

4. Finally stitching path and key to get a unique string, a unique flag for a URL

Later, however, some unexpected results were found, such as the use of Jsonp, where any character exists in the argument. Form: a={"C": [{"D": "8&6"}]}&b=2, resulting in parsing failure

The last discovery can only solve this problem by literal character parsing.

Specific implementation:

1. URL decoding, if the decoding fails, the key character%7b, {,%7d},%20, and empty

2. Initialize iflag=0, key = ', val= ', strsplit= '/' #key之间分隔符, strkey = ' #记录所有key的结果

3. Get a character item, subscript I

4. If Iflag > 0:

if Iflag = = 1:value start position

Istartdakuohao = i

if Iflag = = 1 && item = = ': #跳过空格

Continue

if Iflag = = 1&& item = = ' {':

Iflag = 2

Icountdakuohao = 1

Istardakuohao = I//redefine the starting position of the Jsonp

Continue

if Icountdakuohao > 0:

if Item = = '} ':

Icountdakuohao--

if Icountdakuohao = = 0:

strtmp = Jsondata

List Jsondata

else If Item = = ' {':

Icountdakuohao + +

else If Item = = ' & ' #普通形式的value结尾

iflag = 0

Else

Val.add (item)

else if Key.len = = 0 && Item = = "Continue #过滤掉 start whitespace such as a=1& b=2

if Item = = ' = ' {Iflag =1; #表示已经获得key

If key! = ' _ ':

strkey = strkey + key + strsplit

key = "

}

else {

If Item = = ' & ': #过滤掉没有值得key such as A&b=1

key = "

Else:

Key.add (item) #key里面增加一个字符

}

Sample code:

private static string Getallkeylist (String str,string strsplit) throws Unsupportedencodingexception {string Strke        y = "";        StringBuffer key = new StringBuffer ();        StringBuffer val = new StringBuffer ();        int iflag = 0;        int Icountdakuohao = 0;         int Istartdakuohao = 0;        try {str = java.net.URLDecoder.decode (str, "utf-8");            }catch (Exception exp) {if (Str.indexof ("%7b") >-1) {str = str.replace ("%7b", "{");//            } if (Str.indexof ("%7d") >-1) {str = str.replace ("%7d", "}");            } if (Str.indexof ("%20") >-1) {str = str.replace ("%20", "" ");            }} for (int i = 0; i < str.length (); i++) {Char item = Str.charat (i);                    if (Iflag > 0) {//skip = with {spaces, compatibility is not JSONP keep value at the start of the if (Iflag = = 1) {//value start position                Istartdakuohao = i; }               if (Iflag = = 1 && Item = = ") {continue;                    } if (item = = ' {' && iflag = = 1) {Iflag = 2;                    Icountdakuohao = 1;  Istartdakuohao = i;                Jsonp start position redefine continue;                        } if (Icountdakuohao > 0) {if (item = = '} ') {icountdakuohao--; if (Icountdakuohao = = 0) {String strtmp = str.substring (Istartdakuohao, i+                            1); try{if (Strtmp.indexof ("%22") >-1) {strtmp = strtmp.                                Replace ("%22", "\" "); } if (Strtmp.indexof ("\\x5C") >-1) {strtmp = STRTMP.R                                Eplace ("\\x5C", "\ \"); } if (Strtmp.indexof("\\x22") >-1) {//cast \x22 strtmp = Strtmp.replace ("\\x22", "\" ");                                } Jsonobject obj = Jsonobject.fromobject (strtmp);                                Iterator Iterator = Obj.keys ();                                    while (Iterator.hasnext ()) {String t = (string) iterator.next ();                                    Strkey = strkey + t + strsplit;                                Get the value//System.out.println (Obj.get (t) in JSON); }//End while}catch (Exception exp) {System.out.println (exp.ge                            Tmessage ()); }//System.out.println (integer.tostring (Istartdakuohao) + "---" + strtmp + "---" + integer.t                        Ostring (i));     }}else if (item = = ' {') {                   icountdakuohao++;                } continue;                    } if (item = = ' & ') {//Bfindkey = false;                    Iflag = 0;                Obtained value//System.out.println (str.substring (Istartdakuohao, i));            }else val.append (item);                    }else {//filter out a=1& m=2 if (key.length () = = 0 && Item = = ")                Continue                    if (item = = ' = ') {//Bfindkey = true;                    Iflag = 1;                    String strtmp = key.tostring ();                    if (!strtmp.equals ("_")) Strkey = strkey + strtmp + strsplit;                Key.setlength (0);                    } else {if (item = = ' & ')//filter out keys with no value such as A&b=1 key.setlength (0); else Key.appenD (item); }} if (i = = Str.length ()-1) {//output last value//System.out.println (str.            SUBSTRING (Istartdakuohao, i+1));       }} return strkey;    System.out.println (strkey);        } String str = "param= {\" a\ ":1}&d& m= 78&_=1234"; SYSTEM.OUT.PRINTLN (Getallkeylist (str, "/"));

A bug that can parse only one level of JSON and not multi-level JSON extraction

Web-url parameter Resolution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.