Wednesday 18 October 2017

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

In the above example ‘name=devi&city=Ongole’ is the query string.

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

  String[] queryParams = queryString.split("&");
  Map<String, 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 Map<String, String> parseQueryString(String queryString) throws UnsupportedEncodingException {
  if (queryString == null || queryString.isEmpty()) {
   return Collections.emptyMap();
  }

  String[] queryParams = queryString.split("&");
  Map<String, 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(Map<String, String> map) {
  Set<Map.Entry<String, String>> entrySet = map.entrySet();

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

 private static void parseAndPrintQueryString(String queryString) throws UnsupportedEncodingException {
  Map<String, 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 :


No comments:

Post a Comment