Java URL Query Parse parseQueryString(String query)

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

Description

Parse the given query string into a Map.

License

Open Source License

Declaration

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

Method Source Code

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

import java.io.UnsupportedEncodingException;

import java.net.URLDecoder;

import java.util.LinkedHashMap;
import java.util.Map;

public class Main {
    /**/*from  ww  w . j a v a 2 s.co m*/
     * Parse the given query string into a Map.
     * The order of first appearance is preserved. If a query string entry
     * has no "value" (i.e. no equals sign), its entirety is used as the key.
     */
    public static Map<String, String> parseQueryString(String query) {
        Map<String, String> ret = new LinkedHashMap<String, String>();
        for (String entry : query.split("&")) {
            String[] parts = entry.split("=", 2);
            String key, value;
            try {
                key = URLDecoder.decode(parts[0], "utf-8");
                if (parts.length == 1) {
                    value = null;
                } else {
                    value = URLDecoder.decode(parts[1], "utf-8");
                }
            } catch (UnsupportedEncodingException exc) {
                // Should not happen.
                throw new RuntimeException(exc);
            }
            ret.put(key, value);
        }
        return ret;
    }
}

Related

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