Java URL Query Parse parseQueryString(String query)

Here you can find the source of parseQueryString(String query)

Description

Parses the given query string, and returns a map of the query parameters.

License

Apache License

Parameter

Parameter Description
query the query string to parse

Return

the map of query keys and values

Declaration

public static Map<String, String> parseQueryString(String query) 

Method Source Code

//package com.java2s;
/*//from  w  w  w.j  a  v  a2s  .co  m
 * The Alluxio Open Foundation licenses this work under the Apache License, version 2.0
 * (the ?License??). You may not use this work except in compliance with the License, which is
 * available at www.apache.org/licenses/LICENSE-2.0
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied, as more fully set forth in the License.
 *
 * See the NOTICE file distributed with this work for information regarding copyright ownership.
 */

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

import java.util.HashMap;
import java.util.Map;

public class Main {
    public static final char QUERY_SEPARATOR = '&';
    public static final char QUERY_KEY_VALUE_SEPARATOR = '=';

    /**
     * Parses the given query string, and returns a map of the query parameters.
     *
     * @param query the query string to parse
     * @return the map of query keys and values
     */
    public static Map<String, String> parseQueryString(String query) {
        Map<String, String> queryMap = new HashMap<>();
        if (query == null || query.isEmpty()) {
            return queryMap;
        }
        // The query string should escape '&'.
        String[] entries = query.split(String.valueOf(QUERY_SEPARATOR));

        try {
            for (String entry : entries) {
                // There should be at most 2 parts, since key and value both should escape '='.
                String[] parts = entry.split(String
                        .valueOf(QUERY_KEY_VALUE_SEPARATOR));
                if (parts.length == 0) {
                    // Skip this empty entry.
                } else if (parts.length == 1) {
                    // There is no value part. Just use empty string as the value.
                    String key = URLDecoder.decode(parts[0], "UTF-8");
                    queryMap.put(key, "");
                } else {
                    // Save the key and value.
                    String key = URLDecoder.decode(parts[0], "UTF-8");
                    String value = URLDecoder.decode(parts[1], "UTF-8");
                    queryMap.put(key, value);
                }
            }
        } catch (UnsupportedEncodingException e) {
            // This is unexpected.
            throw new RuntimeException(e);
        }
        return queryMap;
    }
}

Related

  1. parseQueryString(String input)
  2. parseQueryString(String path)
  3. parseQueryString(String path)
  4. parseQueryString(String q, String enc)
  5. parseQueryString(String query)
  6. parseQueryString(String query, boolean ignoreEmpty)
  7. parseQuerystring(String queryString)
  8. parseQueryString(String queryString)
  9. parseQueryString(String queryString)