I receive the response from a service as below. How to parse this into a Map? I first thought of split at whitespace but it doesn’t work as the value might contain spaces e.g. look at the value of SA key in the below response.
One option I thought of is to split at whitespace provided the previous character is a double quote. Not sure how to write the regex for this though.
TX=”0000000000108000001830001″ FI=”” OS=”8″ CI=”QU01SF1S2032″ AW=”SSS” SA=”1525 Windward Concourse”
Parse at quotes. You could even use a regular expression to find each key/value pair, assuming each value is in quotes. My only question would be, what are the rules for if a value contains embedded quotes? (Are they escaped using ‘\’ or such? Regardless, this is not currently accounted for in the below…)
For example:
This will even give you groups #1 and #2 that can be used to provide the key and the value, respectively.
Run this in a loop, using Java’s
Matcher.find()method, until you find all of the pairs.Sample code:
Output: