Java URL Parameter Builder stringToMap(String input)

Here you can find the source of stringToMap(String input)

Description

string To Map

License

Open Source License

Declaration

private static Map<String, Object> stringToMap(String input) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import java.util.HashMap;

import java.util.Map;

public class Main {
    private static Map<String, Object> stringToMap(String input) {
        Map<String, Object> map = new HashMap<String, Object>();
        String[] nameValuePairs = input.split("&");
        for (String nameValuePair : nameValuePairs) {
            String[] nameValue = nameValuePair.split("=");

            try {

                if (isParsable(nameValue[1])) {
                    map.put(URLDecoder.decode(nameValue[0], "UTF-8"), (int) Integer.parseInt(nameValue[1]));
                } else {
                    map.put(URLDecoder.decode(nameValue[0], "UTF-8"),
                            nameValue.length > 1 ? URLDecoder.decode(nameValue[1], "UTF-8") : "");
                }/*from ww  w .j a v  a2s .c o m*/
            } catch (UnsupportedEncodingException e) {
                throw new RuntimeException("This method requires UTF-8 encoding support", e);
            }
        }

        return map;
    }

    public static boolean isParsable(String input) {
        boolean parsable = true;
        try {
            Integer.parseInt(input);
        } catch (NumberFormatException e) {
            parsable = false;
        }
        return parsable;
    }
}

Related

  1. sendGetRequest(String path, Map params, String enc)
  2. sendPostRequest(String path, Map params, String enc)
  3. serializeParameters(final Map parameters)
  4. serializeParameters(final Map params)
  5. stringToMap(String input)
  6. stringUncode(String param)