J'essaie de lire le JSON d'un fichier en Rust qui a les dimensions suivantes :
{
"DIPLOBLASTIC":"Characterizing the ovum when it has two primary germinallayers.",
"DEFIGURE":"To delineate. [Obs.]These two stones as they are here defigured. Weever.",
"LOMBARD":"Of or pertaining to Lombardy, or the inhabitants of Lombardy.",
"BAHAISM":"The religious tenets or practices of the Bahais."
}
Je veux stocker chaque mot et sa description dans un vecteur (c'est pour un jeu de pendu). Je peux lire le fichier s'il est formaté comme ceci :
[
{
"word": "DIPLOBLASTIC",
"description": "Characterizing the ovum when it has two primary germinallayers."
},
{
"word": "DEFIGURE",
"description": "To delineate. [Obs.]These two stones as they are here defigured. Weever."
}
]
Je fais cela en utilisant le code suivant :
#[macro_use]
extern crate serde_derive;
use serde_json::Result;
use std::fs;
#[derive(Deserialize, Debug)]
struct Word {
word: String,
description: String,
}
fn main() -> Result<()> {
let data = fs::read_to_string("src/words.json").expect("Something went wrong...");
let words: Vec<Word> = serde_json::from_str(&data)?;
println!("{}", words[0].word);
Ok(())
}
Cependant, j'essaie de trouver comment conserver le formatage original du fichier JSON sans le convertir en mot et description dans le deuxième exemple JSON.
Est-il possible d'utiliser le format JSON existant ou dois-je le reformater ?