我已經做了一些研究,但我還沒有找到適用於我的案例的工作代碼。我有兩個變量命名test
和test2
,我想將它們放在一個地圖格式[test:valueof(test), test2:valueof(test2)]
通過它的名字作爲字符串訪問變量值(groovy)
我的一段代碼如下:
def test="HELLO"
def test2="WORLD"
def queryText = "\$\$test\$\$ \$\$test2\$\$ this is my test"
def list = queryText.findAll(/\$\$(.*?)\$\$/)
def map = [:]
list.each{
it = it.replace("\$\$", "")
map.putAt(it, it)
}
queryText = queryText.replaceAll(/\$\$(.*?)\$\$/) { k -> map[k[1]] ?: k[0] }
System.out.println(map)
System.out.println(queryText)
輸出:
所需輸出:
"HELLO WORLD this is my test"
我想,我需要這樣的東西map.putAt(it, eval(it))
編輯
這是我得到我輸入的方式。代碼進入'test'
腳本
右邊的那些是腳本中的變量名,左欄是值(稍後將是動態的)
GiLA3,你有機會檢查解決方案嗎? – Rao
RTFTemplates和Message1中的數據是什麼? 'test'中是你現在的groovy腳本嗎? – Rao
RTF模板中的數據是不相關的,'query'屬性是一個帶'$$佔位符$$'的字符串,message1只是一個空框,我將返回腳本處理的結果'queryText'。是的腳本在'test' – GiLA3