Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Convert query string to name value pair (map)

Query String is part of the URL, used to pass the extra information to the request.

Ex
http://example.com/employee?name=devi&city=Ongole


Below snippet converts the query string to a map.
 private static MapString, String> parseQueryString(String queryString) throws UnsupportedEncodingException {
if (queryString == null || queryString.isEmpty()) {
return Collections.EMPTY_MAP;
}

String[] queryParams = queryString.split("&");
MapString, String> map = new HashMap();

for (String queryParam : queryParams) {
int index = queryParam.indexOf('=');
String key = queryParam.substring(0, index);
key = URLDecoder.decode(key, "UTF-8");

String value = null;

if (index > 0) {
value = queryParam.substring(index + 1);
value = URLDecoder.decode(value, "UTF-8");
}

map.put(key, value);
}

return map;
}

Find the below working application.


Test.java
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class Test {

private static MapString, String> parseQueryString(String queryString) throws UnsupportedEncodingException {
if (queryString == null || queryString.isEmpty()) {
return Collections.emptyMap();
}

String[] queryParams = queryString.split("&");
MapString, String> map = new HashMap();

for (String queryParam : queryParams) {
int index = queryParam.indexOf('=');
String key = queryParam.substring(0, index);
key = URLDecoder.decode(key, "UTF-8");

String value = null;

if (index > 0) {
value = queryParam.substring(index + 1);
value = URLDecoder.decode(value, "UTF-8");
}

map.put(key, value);
}

return map;
}

private static void printMap(MapString, String> map) {
SetMap.EntryString, String>> entrySet = map.entrySet();

for (Map.EntryString, String> entry : entrySet) {
System.out.println(entry.getKey() + " : " + entry.getValue());
}
}

private static void parseAndPrintQueryString(String queryString) throws UnsupportedEncodingException {
MapString, String> map = parseQueryString(queryString);
System.out.println("\nFor the query String : " + queryString);
printMap(map);
}

public static void main(String args[]) throws IOException, ClassNotFoundException {

String queryString1 = "name=devi&city=Ongole";
String queryString2 = "name=&city=Ongole";
String queryString3 = "name=devi&city=";
String queryString4 = "name=&city=";

parseAndPrintQueryString(queryString1);
parseAndPrintQueryString(queryString2);
parseAndPrintQueryString(queryString3);
parseAndPrintQueryString(queryString4);
}
}

Output
For the query String : name=devi&city=Ongole
city : Ongole
name : devi

For the query String : name=&city=Ongole
city : Ongole
name :

For the query String : name=devi&city=
city :
name : devi

For the query String : name=&city=
city :
name :

You may like
Miscellaneous
Interview Questions
Encrypt and Decrypt file/stream in Java
Print all the file names recursively
Office URI Scheme
Serialize the object to byte array

Deserialize byte array to java object




This post first appeared on Java Tutorial : Blog To Learn Java Programming, please read the originial post: here

Share the post

Convert query string to name value pair (map)

×

Subscribe to Java Tutorial : Blog To Learn Java Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×