我想要的结果如下,但Rust的结果是另一个。
[123 34 66 111 100 121 34 58 34 97 71 86 115 98 71 56 61 34 125]
golang 示例:
type Response struct {
Body []byte
}
func Test_Marshal(t *testing.T) {
body := "hello"
resp := &Response{
Body:[]byte(body),
}
t.Log("resp:", resp)
result, _ := json.Marshal(resp)
t.Log("result: ", result)
}
去结果:
aa_test.go:17: resp: &{[104 101 108 108 111]}
aa_test.go:19: result: [123 34 66 111 100 121 34 58 34 97 71 86 115 98 71 56 61 34 125]
生锈样品:
use serde_json::{Result, Value};
use serde::Serialize;
#[derive(Serialize, Debug)]
pub struct Response {
pub body: ::std::vec::Vec<u8>,
}
fn main() {
let body_str = "hello".to_string();
let resp = Response {
body: Vec::from(body_str)
};
println!("resp: {:?}", resp);
let result = serde_json::to_vec(&resp).unwrap();
println!("result: {:?}",result)
}
生锈结果
resp: Response { body: [104, 101, 108, 108, 111] }
result: [123, 34, 98, 111, 100, 121, 34, 58, 91, 49, 48, 52, 44, 49, 48, 49, 44, 49, 48, 56, 44, 49, 48, 56, 44, 49, 49, 49, 93, 125]