1

In a service call I'm am getting UrlEncoded String in response like :

first_name=ABC&last_name=XYZ&age=25&mob=999xxxxxxx&email=xxxxx@gmail.com&city=ABC&state=DEF&country=India

Now, I want the above string to be mapped to a Java Object having the variables name matching the keys from above UrlEncoded string.

It can be done by using delimiter "&" and parsing it to java variables one by one but I'm in search of any direct method or library available to do so because the string I'm recieving in response is large so, logically its not good to parse it one by one. Any help will be appreciated

Razneesh
  • 1,147
  • 3
  • 13
  • 29
  • 1
    Possible duplicate of [Parse a URI String into Name-Value Collection](https://stackoverflow.com/questions/13592236/parse-a-uri-string-into-name-value-collection) – Janez Kuhar Nov 14 '19 at 11:54

1 Answers1

2
List<NameValuePair> params = URLEncodedUtils.parse(new URI(url), StandardCharsets.UTF_8);

then init your object

Map<String, String> map = params.stream().collect(Collectors.toMap(NameValuePair::getName, NameValuePair::getValue));

    YourObject yourObject = new ObjectMapper().convertValue(map, YourObject.class);
Valeriy K.
  • 2,616
  • 1
  • 30
  • 53