From 2632b1bbf254b42f6a9e16e09512659cd3376cb1 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Mon, 31 Oct 2022 17:59:50 +0800 Subject: [PATCH] update: json decode and encode demo --- to-json/src/main.rs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/to-json/src/main.rs b/to-json/src/main.rs index a8f5bef..20144fe 100644 --- a/to-json/src/main.rs +++ b/to-json/src/main.rs @@ -1,7 +1,6 @@ use serde_json::{Value}; -fn json_str() -> String { - let json = r#"{ +const JSON_STR: &str = r#"{ "demo": "key_1", "author": "dnomd343", "test": [ @@ -10,13 +9,13 @@ fn json_str() -> String { "345" ] }"#; - return String::from(json); -} fn main() { - let raw = json_str(); - println!("JSON raw content ->\n{}", raw); + println!("JSON raw content ->\n{}", JSON_STR); + + let data: Value = serde_json::from_str(JSON_STR).unwrap(); + println!("{:#?}", data); - let parsed: Value = serde_json::from_str(&raw[..]).unwrap(); - println!("Author -> {}", parsed["author"]); + let ret = serde_json::to_string(&data).unwrap(); + println!("JSON output ->\n{}", ret); }