Parse a string with key=value pair in a map? [duplicate]
Asked Answered
R

2

2

I have below String which is in the format of key1=value1, key2=value2 which I need to load it in a map (Map<String, String>) as key=value so I need to split on comma , and then load cossn as key and 0 its value.

String payload = "cossn=0, itwrqm=200006033213";
Map<String, String> holder =
          Splitter.on(",").trimResults().withKeyValueSeparator("=").split(payload);

I am using Splitter here to do the job for me but for some cases it is failing. For some of my strings, value has some string with equal sign. So for below string it was failing for me:

String payload = "cossn=0, abc=hello/=world";

How can I make it work for above case? For above case key will be abc and value should be hello/=world. Is this possible to do?

Reedbuck answered 23/5, 2016 at 23:2 Comment(4)
how can you make what work exactly? Splitter? Java? There's a bunch of different ways to do this, but maybe no one line function strings.Devilry
adding 2 to the split call should work.Alleyway
After adding 2 to the split, I see this error The method split(CharSequence) in the type Splitter.MapSplitter is not applicable for the arguments (String, int).Reedbuck
@user1950349: littlecegian was referring to String.split, which you aren't using (but which Tomer's answer below uses). MapSplitter.split doesn't have any overload that takes a number of splits, but you can use the limit() API on Splitter to get the same effect.Orientation
E
7

you can add a number to say how many splits you want just add a 2 to split

import java.util.HashMap;

public class HelloWorld{

     public static void main(String []args){
        HashMap<String, String> holder = new HashMap();
        String payload = "cossn=0, abc=hello/=world";
        String[] keyVals = payload.split(", ");
        for(String keyVal:keyVals)
        {
          String[] parts = keyVal.split("=",2);
          holder.put(parts[0],parts[1]);
        }

     }
}
Excited answered 23/5, 2016 at 23:17 Comment(9)
This is the error I am seeing The method split(CharSequence) in the type Splitter.MapSplitter is not applicable for the arguments (String, int).Reedbuck
have you considered using a loop instead?Excited
Can you provide an example how can I do that?Reedbuck
@Reedbuck see my editExcited
I just tried this, and it gave java.lang.ArrayIndexOutOfBoundsException on this line holder.put(parts[0], parts[1]);. Any idea what's wrong?Reedbuck
i think i i may have messed up the quotes try now. it works for meExcited
I would recommend using a Splitter rather than String.split() like you have here, for all the reasons called out in the Splitter documentation: github.com/google/guava/wiki/StringsExplained#splitterOrientation
Will not work in case you have a , in your textInnervate
@Innervate Yes thats correct. But it depends how you are getting your data fed to you. Any good comma separated list would support escaping commas in the text, either by putting quotes around the text which contains a comma, or by putting a backslash or some escaping character before a comma, which you could then implement some logic to decode the escaped commas. If the data you are being fed does not escape commas within the text, it will be extremely difficult to split it correctly, but there may be some ways using other regex type logic.Excited
O
9

You can do this same thing with the Splitter API directly:

Map<String, String> result = Splitter.on(',')
    .trimResults()
    .withKeyValueSeparator(
        Splitter.on('=')
            .limit(2)
            .trimResults())
    .split(input);
Orientation answered 24/5, 2016 at 0:56 Comment(0)
E
7

you can add a number to say how many splits you want just add a 2 to split

import java.util.HashMap;

public class HelloWorld{

     public static void main(String []args){
        HashMap<String, String> holder = new HashMap();
        String payload = "cossn=0, abc=hello/=world";
        String[] keyVals = payload.split(", ");
        for(String keyVal:keyVals)
        {
          String[] parts = keyVal.split("=",2);
          holder.put(parts[0],parts[1]);
        }

     }
}
Excited answered 23/5, 2016 at 23:17 Comment(9)
This is the error I am seeing The method split(CharSequence) in the type Splitter.MapSplitter is not applicable for the arguments (String, int).Reedbuck
have you considered using a loop instead?Excited
Can you provide an example how can I do that?Reedbuck
@Reedbuck see my editExcited
I just tried this, and it gave java.lang.ArrayIndexOutOfBoundsException on this line holder.put(parts[0], parts[1]);. Any idea what's wrong?Reedbuck
i think i i may have messed up the quotes try now. it works for meExcited
I would recommend using a Splitter rather than String.split() like you have here, for all the reasons called out in the Splitter documentation: github.com/google/guava/wiki/StringsExplained#splitterOrientation
Will not work in case you have a , in your textInnervate
@Innervate Yes thats correct. But it depends how you are getting your data fed to you. Any good comma separated list would support escaping commas in the text, either by putting quotes around the text which contains a comma, or by putting a backslash or some escaping character before a comma, which you could then implement some logic to decode the escaped commas. If the data you are being fed does not escape commas within the text, it will be extremely difficult to split it correctly, but there may be some ways using other regex type logic.Excited

© 2022 - 2024 — McMap. All rights reserved.