Created
September 12, 2020 03:06
-
-
Save kuc-arc-f/e381dbcf52ba9b652c2f8778dca47db3 to your computer and use it in GitHub Desktop.
Rust serde_json , jsonからvec変換と 繰り返し処理の例
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Rust serde_json , jsonからvec変換と 繰り返し処理の例 | |
// | |
// [dependencies] | |
// serde = { version = "1.0", features = ["derive"] } | |
// serde_json = "1.0" | |
use serde::{Deserialize, Serialize}; | |
use serde_json::{Result, Value}; | |
fn type_of<T>(_: T) -> &'static str { | |
std::any::type_name::<T>() | |
} | |
#[derive(Serialize, Deserialize, Debug)] | |
struct Point { | |
name: String, | |
age: i32, | |
} | |
// | |
pub fn test() -> Result<()>{ | |
let data = r#" | |
[ | |
{ | |
"name": "John Doe", | |
"age": 43 | |
}, | |
{ | |
"name": "Joh-2", | |
"age": 23 | |
}, | |
{ | |
"name": "Joh-3", | |
"age": 28 | |
} | |
] | |
"#; | |
let deserialized: Vec<Point> = serde_json::from_str(data).unwrap(); | |
//println!( "{:?}" , deserialized ); | |
for row in &deserialized { | |
println!("name={}", row.name ); | |
println!("age={}", row.age ); | |
} | |
Ok(()) | |
} | |
// | |
fn main() { | |
test(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment