0

I have data like this, TITLE is the name of variable and below it is the string data (Please note the data can be different sometimes)

TITLE1
abcd
TITLE2
abcde
TITLE3
acd,sdssds!@#@$#$#%$%$^&**()aas

Now, I want to to send these three to java and want to make a linked map from them I did like this

JAVASCRIPT

var string  = "TITLE1=abcd, TITLE2=abcde, TITLE3=acd,sdssds!@#@$#$#%$%$^&**()aas"

and used it in java as

JAVA

    LinkedHashMap <String, String> selectedCheckBoxMap = new LinkedHashMap <String, String> ();
    String[] pairs = selectedCheckBoxTokens.split (",");

    for (int i = 0; i < pairs.length; i++)
    {
        String pair = pairs[i];
        String[] keyValue = pair.split ("=");
        selectedCheckBoxMap.put (keyValue[0], keyValue[1]);
    }

But this breaks on "," as delimiter as TITLE3 already has character ','

I then used this character instead of "," as delimiter "‡", but this not a good approach.

What should I do ? Should I change it to hashmap in javascript ? But how to convert hashmap from javascript to JAVA directly ? Or should I use some other delimiter ?

The delimiter should be character that we cannot type or ever come across in text.

Thanks,

Aiden

Aiden
  • 460
  • 2
  • 11
  • 29

4 Answers4

0

if your data is that regular you could split on a ", " instead. if spaces are in your value sets, then you need to mark your data values... be that with single/double quotes or some other unique marker.

building a JSON object and then delivering it would likely be a more robust solution.

Jonathan
  • 126
  • 3
  • 12
0

If you use JSON:

var s = {};
s["TITLE1"] = "skladjdklsajdsla";
s["TITLE2"] = "*&^&^%&*,,()*&";
s["TITLE3"] = "acd,sdssds!@#@$#$#%$%$^&**()aas";

That code will create an Java Script Object:

Object { 
TITLE1="skladjdklsajdsla",  
TITLE2="*&^&^%&*,,()*&",  
TITLE3="acd,sdssds!@#@$#$#%$%$^&**()aas"
}

You can parse the Object using a JSON library for Java: http://www.oracle.com/technetwork/articles/java/json-1973242.html

See: How to parse JSON in Java

JSON Standard: http://www.json.org/

Community
  • 1
  • 1
Allan
  • 2,889
  • 2
  • 27
  • 38
0

When generating the string to be send, escape the commas and the equal signs in the values by replacing them with something else like %2C and %3D Then or server side unescape by doing

selectedCheckBoxMap.put (keyValue[0], keyValue[1].replace("%2C",",").replace("%3D","="));
Serg M Ten
  • 5,568
  • 4
  • 25
  • 48
0

Using common characters as a delimiter is error-prawn ever since. You have to escape each delimiter you use and then parse the string by yourself. This means, that you have to call value.replace(/([\\=,])/g, '\\$1') on each entry, before appending it to your datastring.

Even if i would recommend you using JSON, as Alzoid proposed, here is an untested implementation you could use to decode the input (assuming '\' is your escape character):

boolean escaped = false;
boolean waitingForKey = true;
String key = "";
String current = "";
for (int i = 0; i < data.length(); i++) {
    char character = data.charAt(i);
    if (escaped) {
        current += character;
        escaped = false;
        continue;
    }

    if (waitingForKey && Character.isWhitespace(character)) {
        continue;
    } else if (waitingForKey) {
        waitingForKey = false;
    }

    switch (character) {
        case '\\':
            escaped = true;
            break;
        case '=':
            key = current;
            current = "";
            break;
        case ',':
            map.put(key, current);
            current = "";
            key = "";
            waitingForKey = true;
            break;
        default:
            current += character;
    }
}

if (!data.isEmpty()) {
    map.put(key, current);
}
andred
  • 1,204
  • 1
  • 17
  • 29