Rust第一份代码(7.30)

JSON解析

use serde_json::{Result, Value};
use serde::{Deserialize, Serialize};

fn main() {
    println!("Hello, world!");
    let data = r#"
        {
            "host": "John Doe",
            "timeout": 43,
            "comment" : "Simple"
        }"#;
    println!("{}", data);

    /*
     if let Some(result: Method) = serde_json::from_str(data) {
        println!("{}", result);
    }
     */
   

    let result : Result  = serde_json::from_str(data);

    let method = match(result) {
        Ok(data) => data,
        Err(err) => println!("{}", err)
    };
    

    // Parse the string of data into serde_json::Value.
    //serde_json::from_str(data);

}

#[derive(Serialize, Deserialize, Debug)]
pub struct Method {
    pub host : String,
    pub timeout : u32,
    pub comment: String,
}

impl Method {
    fn new(host : String, timeout : u32, comment : String) -> Self {
        Method  {
            host : host,
            timeout : timeout,
            comment
        }
    }
}

你可能感兴趣的:(Rust第一份代码(7.30))