Reputation:
All I have is the request URI from which I have to parse the query params. What I'm doing is adding them to json/hashmap and fetching again like the following
String requestUri = "name=raju&school=abcd&college=mnop&color=black&fruit=mango";
All I have to do is to finally assign it to variables like the following
String name = "raju";
String school = "abcd";
String college = "mnop";
String color = "black";
String fruit = "mango";
So I am parsing the request uri like the following
String[] paramsKV = requestUri.split("&");
JSONArray jsonKVArr = new JSONArray();
for (String params : paramsKV) {
String[] tempArr = params.split("=");
if(tempArr.length>1) {
String key = tempArr[0];
String value = tempArr[1];
JSONObject jsonObj = new JSONObject();
jsonObj.put(key, value);
jsonKVArr.put(jsonObj);
}
}
The another way is to populate the same in hash map and obtain the same. The other way is to match the requestUri
string with regex pattern and obtain the results.
Say for an example to get the value of school
I have to match the values between the starting point of the string school
and the next &
- which doesn't sound good.
I need to construct another hash map like the following from the above results like
Map<String, String> resultMap = new HashMap<String, String>;
resultMap.put("empname", name);
resultMap.put("rschool", school);
resultMap.put("empcollege", college);
resultMap.put("favcolor", color);
resultMap.put("favfruit", fruit);
To make it simple all I have to do is to parse the query param and construct a hashMap by naming the key
filed differently. How could I do it in a simple way? Any help in this is much appreciated.
Upvotes: 8
Views: 15575
Reputation: 22283
You can do it by Jersey library (com.sun.jersey.api.uri.UriComponent
or org.glassfish.jersey.uri.UriComponent
class):
String queryComponent = "name=raju&school=abcd&college=mnop&color=black&fruit=mango";
MultivaluedMap<String, String> params = UriComponent.decodeQuery(queryComponent, true);
Upvotes: 1
Reputation: 1188
Short answer: Every HTTP Client library will do this for you.
Example: Apache HttpClient's URLEncodedUtils class
String queryComponent = "name=raju&school=abcd&college=mnop&color=black&fruit=mango";
List<NameValuePair> params = URLEncodedUtils.parse(queryComponent, Charset.forName("UTF-8"));
They're basically approaching it the same way. Parameter key-value pairs are delimited by &
and the keys from the values by =
so String's split
is appropriate for both.
From what I can tell however your hashmap inserts are mapping new keys to the existing values so there's really no optimization, save maybe for moving to a Java 8 Stream for readability/maintenance and/or discarding the initial jsonArray and mapping straight to the hashmap.
Upvotes: 10
Reputation: 1578
You can use Java 8 and store your data in HashMap in one operation.
Map<String,String> map = Pattern.compile("\\s*&\\s*")
.splitAsStream(requestUri.trim())
.map(s -> s.split("=", 2))
.collect(Collectors.toMap(a -> a[0], a -> a.length > 1 ? a[1]: ""));
Upvotes: 3
Reputation: 9650
Here is another possible solution:
Pattern pat = Pattern.compile("([^&=]+)=([^&]*)");
Matcher matcher = pat.matcher(requestUri);
Map<String,String> map = new HashMap<>();
while (matcher.find()) {
map.put(matcher.group(1), matcher.group(2));
}
System.out.println(map);
Upvotes: 5
Reputation: 2480
Use jackson json parser. Maven dependency:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.3</version>
</dependency>
Now use ObjectMapper to create Map from json string:
ObjectMapper mapper = new ObjectMapper();
Map list = mapper.readValue(requestUri, Map.class);
Upvotes: -3