1

游乐场链接

use serde_json::json; // 1.0.57
fn main() {

let users = vec![Users {
  id : 10,
  username : "test".to_string(),
  password : "pass".to_string()
  }];
 for user in &users {
     println!("I print  id:{},password:{},username:{} ",user.id, user.password, user.username);
 }
 println!("json_serde prints {}",json!(&users));
 
 let serialized = serde_json::to_string(&users).unwrap();
 println!("Different serde: {}",serialized);
}
#[derive(Serialize, Deserialize)]
pub struct Users {
 pub id: i32,
 pub username: String,
 pub password: String,
}

它工作得很好,但在我的服务器上我得到了这个

I print  id:4,password:test, username:test
json_serde prints [{"id":4,"password":"test\r\n","username":"test\r"}]

唯一的区别是我从数据库中获取数据

柴油查询

    let users = users
    .filter(id.eq(p_id))
    .limit(10)
    .load::<Users>(&connection)
    .expect("Error loading posts"),

model.rs 中的实际用户

    #[derive(Queryable)]
    #[derive(Serialize, Deserialize)]
    pub struct Users {
        pub id: i32,
        pub username: String,
        pub password: String,
    }

谢马尔

 table! {
        users (id) {
            id -> Int4,
            username -> Varchar,
            password -> Varchar,
        }
    }
4

1 回答 1

1

我忘记修剪我的输入,所以 serde_json 是正确的

于 2020-09-19T16:25:34.273 回答