1 回答

TA贡献1818条经验 获得超8个赞
您可以检查正则表达式以查找模式“%”作为您的问题。但是,由于字符串很简单,并且您只需要子字符串,因此请替换 %paramName 的值。您只能使用字节。更换以解决您的问题。(我没有使用字符串。替换,因为不需要解析为字符串)。
package main
import (
"encoding/json"
"log"
"bytes"
)
func main() {
byteValue := []byte(`{
"programBinary" : "/usr/bin/foo",
"extraArgs" : " --arg1=%argumentOne --arg2=%argumentTwo",
"argumentOne" : "foo",
"argumentTwo" : "bar"
}`)
var rawMap map[string]json.RawMessage;
rawErr := json.Unmarshal(byteValue, &rawMap)
if(rawErr != nil) {
log.Printf("JSON Marshalling error (raw): %s\n" , rawErr.Error());
}
byteResult := make([]byte, len(byteValue))
copy(byteResult, byteValue)
for key, value := range rawMap {
stringEscaped := bytes.Replace([]byte(value), []byte("\""), []byte(""), 2);//Can either remove quotes, replace them with single quotes, or escape with \" here
byteResult = bytes.Replace(byteResult, []byte("%"+key), stringEscaped, 1)
}
log.Printf("Result:\n %s", string(byteResult))
}
- 1 回答
- 0 关注
- 145 浏览
添加回答
举报