3

Is there a utility in java to de parameterize the object parameterized by Jquery param() method. I got the following code working for now( I can parse the returned map to a Json object). Don't know how robust it will be. Is there a util in java just like js one here

public class Util {

/**
 * Deparameterize to original json form the query params coming from the jquery.param() method.
 * @param query the query string key value(array of values if same key repeated) pairs.
 * @return
 */
public static Map<String, Object> deParam(Map<String, String[]> query) {

    Map<String, Object> map = new HashMap<>();
    List<Map<String,Object>> toArray=new ArrayList<>();

    for (String key : query.keySet()) {
        //ex key $and[0][price][$lt]
        String value=query.get(key)[0];
        key = key.replace("]", "");

        String[] split = key.split("\\[");

        Map<String, Object> grandParent =null;
        Map<String, Object> parent =map;
        String parentName="root";
        Map<String, Object> child = null;
        String lastSkey=null;

        for (String skey : split) {
            Object me = parent.get(skey);
            if(skey.matches("[0-9]+")){
                //if array key
                toArray.add(parent);
                parent.put("parent",grandParent);
                parent.put("myName", parentName );
            }
            if (me == null) {
                child=new HashMap<>();
                parent.put(skey, child);
                grandParent=parent;
                parent=child;
                parentName=skey;
            } else {
                grandParent=parent;
                parent = (Map<String, Object>) me;
                parentName=skey;
            }
            lastSkey=skey;
        }
        if(value.matches("[0-9]+\\.?[0-9]+")){
            Double numVal=new Double(value);
            grandParent.put(lastSkey,numVal);
        }else {
            grandParent.put(lastSkey,value);
        }

    }

    for (Map<String, Object> arrayMap : toArray) {
        Map<String, Object> myParent = (Map<String, Object>) arrayMap.remove("parent");
        String myName = (String) arrayMap.remove("myName");
        if(myParent!=null&&myName!=null){
            myParent.put(myName, arrayMap.values());
        }else if(myName!=null && myName.equals("root")){
            map.put("list", arrayMap.values());
        }
    }

    return map;
}

}

3
  • 1
    Can you provide some input examples for the Map<String, String[]> query param. Commented Mar 30, 2020 at 12:11
  • 1
    Also the desired output based on the inputs. Examples of all possible kinds would be helpful. E.g. { width:1680, height:1050, test: [1,2,3] } - param -> width=1680&height=1050&test%5B%5D=1&test%5B%5D=2&test%5B%5D=3 - decodeURI -> width=1680&height=1050&test[]=1&test[]=2&test[]=3 Commented Mar 30, 2020 at 12:26
  • Isn't this just standard URL parameter parsing? stackoverflow.com/questions/5902090/… for example but there are many others. Commented Mar 30, 2020 at 23:00

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.