通过例子学习Rust

49.8.1 Json

serialize::json::Json is an enum capable of containing all of the JSON value types. It is Rust's way of representing JSON documents.

Json can be decoded from a JSON string using seralize::json::from_str:

extern crate serialize; use serialize::json; fn main() { for &json_str in [ // Valid JSON r#"{"name": "London", "lat": 51.507222, "lon": -0.1275}"#, // Not a valid JSON, will produce an error r#"{"name": "Prague" "lat": 50.082542, "lon": 14.425992}"#, // FIXME ^ a comma is missing here ].iter() { // `from_str` decodes JSON from a string and returns a `Result` // containing either a `Json` enum or an error match json::from_str(json_str) { Ok(json) => println!("json::from_str(): {}", json), Err(err) => println!("json::from_str(): {}", err), }; } }