Java URL Query Parse parseQuerystring(String queryString)

Here you can find the source of parseQuerystring(String queryString)

Description

Parse a querystring into a map of key/value pairs.

License

Open Source License

Parameter

Parameter Description
queryString the string to parse (without the '?')

Return

key/value pairs mapping to the items in the querystring

Declaration

public static Map<String, String> parseQuerystring(String queryString) 

Method Source Code

//package com.java2s;
/**/* w  w  w  .j av  a2s . c o  m*/
 * Mule Google Api Commons
 *
 * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 *
 * The software in this package is published under the terms of the CPAL v1.0
 * license, a copy of which has been included with this distribution in the
 * LICENSE.txt file.
 */

import java.io.UnsupportedEncodingException;

import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

public class Main {
    /**
     * Parse a querystring into a map of key/value pairs.
     *
     * @param queryString the string to parse (without the '?')
     * @return key/value pairs mapping to the items in the querystring
     */
    public static Map<String, String> parseQuerystring(String queryString) {
        Map<String, String> map = new HashMap<String, String>();
        if ((queryString == null) || (queryString.equals(""))) {
            return map;
        }
        String[] params = queryString.split("&");
        for (String param : params) {
            try {
                String[] keyValuePair = param.split("=", 2);
                String name = URLDecoder.decode(keyValuePair[0], "UTF-8");
                if (name == "") {
                    continue;
                }
                String value = keyValuePair.length > 1 ? URLDecoder.decode(
                        keyValuePair[1], "UTF-8") : "";
                map.put(name, value);
            } catch (UnsupportedEncodingException e) {
                // ignore this parameter if it can't be decoded
            }
        }
        return map;
    }
}

Related

  1. parseQueryString(String query)
  2. parseQueryString(String query)
  3. parseQueryString(String query, boolean ignoreEmpty)
  4. parseQueryString(String queryString)
  5. parseQueryString(String queryString)
  6. parseQueryString(String queryString, Map params)
  7. parseQueryStringEx(String queryString)
  8. parseURLQuery(String query)