0

I have a string as follows and I'm splitting the parameters (TransactionType, ServiceID, PaymentID, OrderNumber, Amount) as below.

String response = "TransactionType=SALE&ServiceID=TV3&PaymentID=PYID2016061501417701&OrderNumber=2016061501417701&Amount=235.00"

String splitParams[] = res.split("&");
        String TransactionType = splitParams[0].substring(splitParams[0]
                .indexOf("=") + 1);
        String ServiceID = splitParams[1].substring(splitParams[1]
                .indexOf("=") + 1);
        String PaymentID = splitParams[2].substring(splitParams[2]
                .indexOf("=") + 1);
        String OrderNumber = splitParams[3].substring(splitParams[3]
                .indexOf("=") + 1);
        String amount = splitParams[4].substring(splitParams[4]
                .indexOf("=") + 1);

But the problem is these parameters are not sent in order and sometimes some parameters are not sent. Is there any way to split the parameters in string using java based on parameter name?

dmaprasad
  • 301
  • 2
  • 7
  • 17
  • 2
    Why don't you create a `Map` instead? – Kayaman Jun 15 '16 at 11:10
  • As I understand, you have an URL fragment. I think you could find plenty of answers here: http://stackoverflow.com/questions/13592236/parse-a-uri-string-into-name-value-collection , most of them deal with url, but it should fit also to only parameter part. – Adomas Jun 15 '16 at 11:11

2 Answers2

4

You could use guavas Splitter like this:

Map<String, String> responseMap = Splitter.on("&")
        .omitEmptyStrings()
        .trimResults()
        .withKeyValueSeparator("=")
        .split(response);

And then you could get the values like that:

String TransactionType = responseMp.get("TransactionType ");
Frank
  • 2,036
  • 1
  • 20
  • 32
  • I'm getting error "The method withKeyValueSeparator(String) is undefined for the type Splitter". I have added "com.google.guava_1.6.0.jar". Can u please help with that? – dmaprasad Jun 15 '16 at 11:31
  • @dmaprasad this function is only from Google Guava, if you want to use it you need to import it like import com.google.common.base.Splitter; – Seek Addo Jun 15 '16 at 11:37
  • I have imported like com.google.common.base.Splitter;. But same result. not working. do u have any idea. Really appreciate help – dmaprasad Jun 15 '16 at 11:42
  • I use guava-18.0.jar. The maven-dependency is ` com.google.guava guava18.0 ` – Frank Jun 15 '16 at 12:23
  • Thank you very much. By replacing guava-18.0.jar with com.google.guava_1.6.0.jar is worked. Really appreciate your help. – dmaprasad Jun 15 '16 at 13:53
2

Try this:

String response = "TransactionType=SALE&ServiceID=TV3&PaymentID=PYID2016061501417701&OrderNumber=2016061501417701&Amount=235.00";

Map<String,String> responseMap = Arrays.asList(response.split("&")).stream().
     map(v-> v.split("=")).collect(Collectors.toMap(a-> a[0],a ->a[1]));



System.out.println(responseMap);

output:

{TransactionType=SALE, Amount=235.00, PaymentID=PYID2016061501417701, OrderNumber=2016061501417701, ServiceID=TV3}

Seek Addo
  • 1,871
  • 2
  • 18
  • 30