`rtserver id` turns to `rtserver id` in java string

I have this code:

public void foo (){
        String script =
                "var aLocation = {};" +
                        "var aOffer = {};" +

                        "var aAdData = " +
                        "{ " +
                        "location:  aLocation, " +
                        "offer:    aOffer " +
                        " };" +

                        "var aClientEnv = " +
                        " { " +
                        "    sessionid:     \"\", " +
                        "   cookie:        \"\", " +
                        "   rtserver-id: 1,   " +
                        "       lon:           34.847, " +
                        "       lat:           32.123, " +
                        "       venue:         \"\", " +
                        "    venue_context: \"\", " +

                        "    source:        \"\"," +   // One of the following (string) values: ADS_PIN_INFO,
                        // ADS_0SPEED_INFO, ADS_LINE_SEARCH_INFO,
                        // ADS_ARROW_NEARBY_INFO, ADS_CATEGORY_AUTOCOMPLETE_INFO,
                        // ADS_HISTORY_LIST_INFO
                        // (this field is also called "channel")

                        "    locale:        \"\"" + // ISO639-1 language code (2-5 characters), supported formats:
                        " };" +


                        "W.setOffer(aAdData, aClientEnv);";

            javascriptExecutor.executeScript(script);
}

I have two q:

  1. when I debug and copy script value I see a member rtserver - id instead of rtserver-id how can it be? the code throws an exception because of this.

Even if i remove this rtserver-id member (and there is not exception thrown)

I evaluate aLocation in this browser console and get "variable not defined". How can this be?

enter image description here

Jon Skeet
people
quotationmark

rtserver-id isn't a valid identifier - so if you want it as a field/property name, you need to quote it. You can see this in a Chrome Javascript console, with no need for any Java involved:

> var aClientEnv = { sessionId: "", rtserver-id: 1 };

Uncaught SyntaxError: Unexpected token -

> var aClientEnv = { sessionId: "", "rtserver-id": 1 };

undefined

> aClientEnv

Object {sessionId: "", rtserver-id: 1}

Basically I don't think anything's adding spaces - you've just got an invalid script. You can easily add the quotes in your Java code:

"   \"rtserver-id\": 1,   " +

people

See more on this question at Stackoverflow