[英]golang : json.unmarshall fails when there are new lines in source data
我正在通過 golang 運行以下代碼片段:
package main
import (
"encoding/json"
"fmt"
)
type response struct {
Data1 string `json:"data1"`
Data2 string `json:"data2"`
}
func main() {
//source data
str := `{"data1": "this is
test data 1",
"data2": "this is test data 2"}`
res := response{}
fmt.Println("json string is \n")
fmt.Println(str)
json.Unmarshal([]byte(str), &res)
fmt.Println("structure output is \n")
fmt.Println(res)
}
我在 res 響應結構中期待 output,但是我得到空響應。
當前 Output:
json string is
{"data1": "this is
test data 1",
"data2": "this is test data 2"}
structure output is
{ }
有沒有辦法通過 json.Unmarshal 獲得正確的結構 output?
json.Unmarshal
被忽略的錯誤是
Error invalid character '\n' in string literal
如果您想在 data1 中換行,請將str
更改為
str := `{"data1": "this is \n test data 1",
"data2": "this is test data 2"}`
然后 output 將是
structure output is
{this is
test data 1 this is test data 2}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.