1 回答

TA貢獻1818條經驗 獲得超8個贊
您可以檢查正則表達式以查找模式“%”作為您的問題。但是,由于字符串很簡單,并且您只需要子字符串,因此請?zhí)鎿Q %paramName 的值。您只能使用字節(jié)。更換以解決您的問題。(我沒有使用字符串。替換,因為不需要解析為字符串)。
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 關注
- 177 瀏覽
添加回答
舉報