Created
September 11, 2020 09:13
-
-
Save kuc-arc-f/ed58efa32591815786661b32d6501d75 to your computer and use it in GitHub Desktop.
Rust serde_json ,jsonから、 配列変換(array * object)で、繰り返し処理の例
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から、 配列変換(array * object)で、繰り返し処理の例 | |
// | |
// [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>() | |
} | |
// | |
pub fn test4() -> Result<()>{ | |
let data = r#" | |
[ | |
{ | |
"name": "John Doe", | |
"age": 43 | |
}, | |
{ | |
"name": "Joh-2", | |
"age": 23 | |
}, | |
{ | |
"name": "Joh-3", | |
"age": 28 | |
} | |
] | |
"#; | |
// let deserialized: Value = serde_json::from_str(data)?; | |
let deserialized: Value = serde_json::from_str(data).unwrap(); | |
// println!( "{:?}" , deserialized ); | |
let max= 3; | |
for i in 0..max { | |
println!( "{}, a={}" , deserialized[i]["name"], deserialized[i]["age"] ); | |
} | |
Ok(()) | |
} | |
// | |
fn main() { | |
//println!("Hello, world!"); | |
test4(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment