gpt4 book ai didi

java - java字符串中`rtserver-id`变成`rtserver - id`

转载 作者:行者123 更新时间:2023-12-02 04:59:15 29 4
gpt4 key购买 nike

我有这个代码:

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并得到“变量未定义”。怎么会这样?

enter image description here

最佳答案

rtserver-id 不是有效的标识符 - 因此如果您希望将其作为字段/属性名称,则需要将其引用。您可以在 Chrome Javascript 控制台中看到这一点,无需涉及任何 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,   " +

关于java - java字符串中`rtserver-id`变成`rtserver - id`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28454823/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com