`rtserver-id` 在 java 字符串中变为 `rtserver - id`

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

我有这个代码:

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);
}

我有两个问题:

  1. 当我调试和复制 script 值时,我看到一个成员 rtserver - id 而不是 rtserver-id 怎么会这样?代码因此抛出异常。

即使我删除这个 rtserver-id 成员(并且没有抛出异常)

我在此浏览器控制台中评估 aLocation 并得到 "variable not defined"。怎么会这样?

rtserver-id 不是有效的标识符 - 因此如果您希望它作为 field/property 名称,您需要将其引用。您可以在 Chrome Java 脚本控制台中看到这一点,不需要任何 Java 涉及:

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

Uncaught SyntaxError: Unexpected token -

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

undefined

> aClientEnv

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

基本上我不认为有任何东西在添加空格 - 你只是得到了一个无效的脚本。您可以轻松地在 Java 代码中添加引号:

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