Consider code:
 Map prJsonData = readJSON text: '{}'
 prJsonData.head = "release/${NEW_TAG}" as String
 prJsonData.title = "Release ${NEW_TAG}"
 writeJSON(file: 'create-pr.json', json: prJsonData, pretty: 4)
Run Code Online (Sandbox Code Playgroud)
and output
{
    "head": "release/v1.0.2",
    "title":     {
        "bytes":         [
            82,
            101,
            97
        ],
        "strings":         [
            "Release ",
            ""
        ],
        "valueCount": 1,
        "values": ["v1.0.2"]
    }
}
Run Code Online (Sandbox Code Playgroud)
Why is it that specifying as String changes the output such that interpolation works but without this the output appears to be some sort of complex type.
当您$在字符串内部使用替换其中的变量时,您实际上并没有得到一个 Java 字符串,而是一个GString. 你的 JSON 序列化器然后只是序列化它:
groovy:000> a=1
===> 1
groovy:000> s="$a"
===> 1
groovy:000> s.getClass()
===> class org.codehaus.groovy.runtime.GStringImpl
groovy:000> s.properties
===> [values:[1], class:class org.codehaus.groovy.runtime.GStringImpl, bytes:[49], strings:[, ], valueCount:1]
Run Code Online (Sandbox Code Playgroud)
.toString()当消费者接受任何对象时,通常需要使用或转换为字符串,因此这会有所不同。根据您的 JSON-Library,为 GString 添加您自己的序列化程序可能是一个好主意,以防止出现这样的混淆。
|   归档时间:  |  
           
  |  
        
|   查看次数:  |  
           910 次  |  
        
|   最近记录:  |