How do I parse a JSON File?

后端 未结 4 1559
梦如初夏
梦如初夏 2020-12-13 12:28

I have this so far in my goal to Parse this JSON data in Rust:

extern crate rustc_serialize;
use rustc_serialize::json::Json;
use std::fs::File;
use std::io:         


        
4条回答
  •  情歌与酒
    2020-12-13 12:57

    There is a brief and complete example of how to read JSON from file in serde_json::de::from_reader docs.

    Here is a short snippet for:

    • reading a file
    • parsing its contents as a JSON
    • and extracting a field with the desired key

    Enjoy:

    let file = fs::File::open("text.json")
        .expect("file should open read only");
    let json: serde_json::Value = serde_json::from_reader(file)
        .expect("file should be proper JSON");
    let first_name = json.get("FirstName")
        .expect("file should have FirstName key");
    

提交回复
热议问题