From 69bb8782c8b7c666630da85e4ecaec4ee7453536 Mon Sep 17 00:00:00 2001 From: bzmplan <15249938392@163.com> Date: Fri, 16 Jun 2023 09:01:59 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=B0=86=E6=95=B0=E6=8D=AE=E5=AD=98?= =?UTF-8?q?=E5=82=A8=E5=88=B0json=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data/data.json | 38 ++++++++++++++++++ src/main.rs | 107 ++++++++++++++++++++++++++++++++++++------------- 2 files changed, 118 insertions(+), 27 deletions(-) create mode 100644 data/data.json diff --git a/data/data.json b/data/data.json new file mode 100644 index 0000000..d260703 --- /dev/null +++ b/data/data.json @@ -0,0 +1,38 @@ +[ + {"id":1,"count":0}, + {"id":2,"count":0}, + {"id":3,"count":0}, + {"id":4,"count":0}, + {"id":5,"count":0}, + {"id":6,"count":0}, + {"id":7,"count":0}, + {"id":8,"count":0}, + {"id":9,"count":0}, + {"id":10,"count":0}, + {"id":11,"count":0}, + {"id":12,"count":0}, + {"id":13,"count":0}, + {"id":14,"count":0}, + {"id":15,"count":0}, + {"id":16,"count":0}, + {"id":17,"count":0}, + {"id":18,"count":0}, + {"id":19,"count":0}, + {"id":20,"count":0}, + {"id":21,"count":0}, + {"id":22,"count":0}, + {"id":23,"count":0}, + {"id":24,"count":0}, + {"id":25,"count":0}, + {"id":26,"count":0}, + {"id":27,"count":0}, + {"id":28,"count":0}, + {"id":29,"count":0}, + {"id":30,"count":0}, + {"id":31,"count":0}, + {"id":32,"count":0}, + {"id":33,"count":0}, + {"id":34,"count":0}, + {"id":35,"count":0} + +] \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 1fb54e9..e8f5c74 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,6 +10,7 @@ extern crate native_windows_gui as nwg; use nwd::NwgUi; use nwg::NativeUi; use rand::Rng; +use serde_json::Value; #[derive(Default, NwgUi)] pub struct BasicApp { @@ -93,33 +94,66 @@ impl BasicApp { let student = rng.gen_range(0..stduents.len()); let id = usize_to_i32(student); - if check_file(id) { - - let count = read_file(id); - let _ = write_file(id, count.to_string()); - nwg::modal_info_message( - &self.window, - "恭喜", - &format!( - "恭喜 {} !\n目前你已经被抽到 {} 次", - stduents[student], - string_to_i32(count) + 1 - ), - ); - } else { - let _ = create_file(id); - let count = read_file(id); - let _ = write_file(id, count.to_string()); - nwg::modal_info_message( - &self.window, - "恭喜", - &format!( - "恭喜 {} !\n目前你已经被抽到 {} 次", - stduents[student], - string_to_i32(count) + 1 - ), - ); + //first version 用文本文件存贮数据 + // if check_file(id) { + + // let count = read_file(id); + // let _ = write_file(id, count.to_string()); + // nwg::modal_info_message( + // &self.window, + // "恭喜", + // &format!( + // "恭喜 {} !\n目前你已经被抽到 {} 次", + // stduents[student], + // string_to_i32(count) + 1 + // ), + // ); + // } else { + // let _ = create_file(id); + // let count = read_file(id); + // let _ = write_file(id, count.to_string()); + // nwg::modal_info_message( + // &self.window, + // "恭喜", + // &format!( + // "恭喜 {} !\n目前你已经被抽到 {} 次", + // stduents[student], + // string_to_i32(count) + 1 + // ), + // ); + // } + + //second version 用json文件存储数据 + + let mut file = File::open("data/data.json").expect("Unable to open file"); + let mut contents = String::new(); + file.read_to_string(&mut contents) + .expect("Unable to read data from file"); + + // 将JSON字符串解析为serde_json::Value + let mut data: Value = serde_json::from_str(&contents).unwrap(); + + // 修改JSON数组中某个对象的某个值 + if let Some(people) = data.as_array_mut() { + if let Some(person) = people.get_mut(student) { + if let Some(count) = person["count"].as_u64() { + person["count"] = Value::from(count + 1); + } + } } + + let count = get_info(data.to_owned(), student); + + // 将修改后的JSON对象序列化为字符串并写回文件 + let json_string = serde_json::to_string_pretty(&data).unwrap(); + let mut file = File::create("data/data.json").expect("Unable to create file"); + file.write_all(json_string.as_bytes()) + .expect("Unable to write data to file"); + nwg::modal_info_message( + &self.window, + "恭喜", + &format!("恭喜 {} !\n目前你已经被抽到 {} 次", stduents[student],count), + ); } //当软件关闭的时候 fn when_app_close(&self) { @@ -138,7 +172,7 @@ fn main() { use std::{ fs::{self, File}, - io::{self, Write}, + io::{self, Read, Write}, }; fn read_file(id: i32) -> String { @@ -182,6 +216,25 @@ fn check_file(id: i32) -> bool { result } +fn get_info(mut data:Value,student:usize) ->u64{ + //读取数据 + if let Some(people) = data.as_array_mut() { + if let Some(person) = people.get_mut(student) { + if let Some(count) = person["count"].as_u64() { + count + } + else { + todo!(); + } + }else { + todo!(); + } + } + else { + todo!(); + } +} + fn usize_to_i32(usize: usize) -> i32 { usize as i32 } From f44bc12820d864b51157f6ca301aeae3944fb489 Mon Sep 17 00:00:00 2001 From: bzmplan <15249938392@163.com> Date: Fri, 16 Jun 2023 09:04:18 +0800 Subject: [PATCH 2/2] new --- data/data.json | 252 ++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 216 insertions(+), 36 deletions(-) diff --git a/data/data.json b/data/data.json index d260703..8c873a6 100644 --- a/data/data.json +++ b/data/data.json @@ -1,38 +1,218 @@ [ - {"id":1,"count":0}, - {"id":2,"count":0}, - {"id":3,"count":0}, - {"id":4,"count":0}, - {"id":5,"count":0}, - {"id":6,"count":0}, - {"id":7,"count":0}, - {"id":8,"count":0}, - {"id":9,"count":0}, - {"id":10,"count":0}, - {"id":11,"count":0}, - {"id":12,"count":0}, - {"id":13,"count":0}, - {"id":14,"count":0}, - {"id":15,"count":0}, - {"id":16,"count":0}, - {"id":17,"count":0}, - {"id":18,"count":0}, - {"id":19,"count":0}, - {"id":20,"count":0}, - {"id":21,"count":0}, - {"id":22,"count":0}, - {"id":23,"count":0}, - {"id":24,"count":0}, - {"id":25,"count":0}, - {"id":26,"count":0}, - {"id":27,"count":0}, - {"id":28,"count":0}, - {"id":29,"count":0}, - {"id":30,"count":0}, - {"id":31,"count":0}, - {"id":32,"count":0}, - {"id":33,"count":0}, - {"id":34,"count":0}, - {"id":35,"count":0} - + { + "id": 1, + "count": 0 + }, + { + "id": 2, + "count": 0 + }, + { + "id": 3, + "count": 0 + }, + { + "id": 4, + "count": 0 + }, + { + "id": 5, + "count": 0 + }, + { + "id": 6, + "count": 0 + }, + { + "id": 7, + "count": 0 + }, + { + "id": 8, + "count": 0 + }, + { + "id": 9, + "count": 0 + }, + { + "id": 10, + "count": 0 + }, + { + "id": 11, + "count": 0 + }, + { + "id": 12, + "count": 0 + }, + { + "id": 13, + "count": 0 + }, + { + "id": 14, + "count": 0 + }, + { + "id": 15, + "count": 0 + }, + { + "id": 16, + "count": 0 + }, + { + "id": 17, + "count": 0 + }, + { + "id": 18, + "count": 0 + }, + { + "id": 19, + "count": 0 + }, + { + "id": 20, + "count": 0 + }, + { + "id": 21, + "count": 0 + }, + { + "id": 22, + "count": 0 + }, + { + "id": 23, + "count": 0 + }, + { + "id": 24, + "count": 0 + }, + { + "id": 25, + "count": 0 + }, + { + "id": 26, + "count": 0 + }, + { + "id": 27, + "count": 0 + }, + { + "id": 28, + "count": 0 + }, + { + "id": 29, + "count": 0 + }, + { + "id": 30, + "count": 0 + }, + { + "id": 31, + "count": 0 + }, + { + "id": 32, + "count": 0 + }, + { + "id": 33, + "count": 0 + }, + { + "id": 34, + "count": 0 + }, + { + "id": 35, + "count": 0 + }, + { + "id": 36, + "count": 0 + }, + { + "id": 37, + "count": 0 + }, + { + "id": 38, + "count": 0 + }, + { + "id": 39, + "count": 0 + }, + { + "id": 40, + "count": 0 + }, + { + "id": 41, + "count": 0 + }, + { + "id": 42, + "count": 0 + }, + { + "id": 43, + "count": 0 + }, + { + "id": 44, + "count": 0 + }, + { + "id": 45, + "count": 0 + }, + { + "id": 46, + "count": 0 + }, + { + "id": 47, + "count": 0 + }, + { + "id": 48, + "count": 0 + }, + { + "id": 49, + "count": 0 + }, + { + "id": 50, + "count": 0 + }, + { + "id": 51, + "count": 0 + }, + { + "id": 52, + "count": 0 + }, + { + "id": 53, + "count": 0 + }, + { + "id": 54, + "count": 0 + } ] \ No newline at end of file