首页 > 其他分享 >实例-rust-将数据写入json文件

实例-rust-将数据写入json文件

时间:2022-09-18 00:11:40浏览次数:77  
标签:use 写入 Write json let vvx1 Data rust

Cargo.toml

[package]
name = "rust-example5"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
peroxide = "0.31.6"
serde = { version = "1.0.133", features = ["derive"] }
serde_json = "1.0.75"

main.rs

#![allow(non_snake_case)]
// #[macro_use]
extern crate peroxide;
use peroxide::fuga::*;
use std::fs::File;
use std::io::prelude::*;
use std::io::BufReader;
use std::io::BufWriter;
//
use serde::Deserialize;  // Input
use serde::Serialize;    // Output
use serde_json::from_reader;
use serde_json::to_string_pretty;


fn main() -> Result<(), Box<dyn Error>>  {


    let x1_ = 1;
    let vx1_ = vec![1.0,2.0,3.0];
    let vvx1_ = vec![vec![1.0,2.0,3.0]];

    let Data_Write_To_Input = WriteToInput {
        x1:&x1_,
        vx1: &vx1_,
        vvx1: &vvx1_,
    };
    let Data_Write_To_Input_Out = to_string_pretty::<WriteToInput>(&Data_Write_To_Input)?;
    let mut Data_Write_To_Input_In = BufWriter::new(File::create("input.json")?);
    write!(&mut Data_Write_To_Input_In, "{}", Data_Write_To_Input_Out)?;



    /////从input.json文件读取输入数据
    let Data_Read_From_Input = BufReader::new(File::open("input.json")?);
    let InputData {
        x1,
        vx1,
        vvx1,
    } = from_reader::<_, InputData>(Data_Read_From_Input)?;

    println!("{:?},{:?},{:?}", x1,vx1,vvx1);



    // /////将结果写入ouput.json
    // let Data_Write_To_Output = OutputData {
    //     x1:&x1,
    //     vx1: &vx1,
    //     vvx1: &vvx1,
    // };
    // let Data_Write_To_Output_Out = to_string_pretty::<OutputData>(&Data_Write_To_Output)?;
    // let mut Data_Write_To_Output_In = BufWriter::new(File::create("output.json")?);
    // write!(&mut Data_Write_To_Output_In, "{}", Data_Write_To_Output_Out)?;
    // println!("ok!");
    Ok(())
    
}


/////////////////////////////////////////////////////

#[derive(Serialize, Debug)]
struct WriteToInput<'a> {
    x1:&'a i32,
    vx1: &'a Vec<f64>,
    vvx1: &'a Vec<Vec<f64>>,
}



#[derive(Deserialize)]
struct InputData {
    x1: i32,
    vx1: Vec<f64>,
    vvx1: Vec<Vec<f64>>,
}

#[derive(Serialize, Debug)]
struct OutputData<'a> {
    x1:&'a i32,
    vx1: &'a Vec<f64>,
    vvx1: &'a Vec<Vec<f64>>,
}

标签:use,写入,Write,json,let,vvx1,Data,rust
From: https://www.cnblogs.com/Nazorine/p/16704006.html

相关文章

  • 实例-rust-string和bytes转换
    Cargo.toml[package]name="rust-example9"version="0.1.0"edition="2021"#Seemorekeysandtheirdefinitionsathttps://doc.rust-lang.org/cargo/refere......
  • 实例-rust-将struct写入json文件
    cargo.toml[package]name="rust-example5"version="0.1.0"edition="2021"#Seemorekeysandtheirdefinitionsathttps://doc.rust-lang.org/cargo/refere......
  • 实例-rust-从json文件中读取数据并将输出写入json文件
    Cargo.toml[package]name="rust-example5"version="0.1.0"edition="2021"#Seemorekeysandtheirdefinitionsathttps://doc.rust-lang.org/cargo/refere......
  • 实例-rust-线性代数运算
    Cargo.toml[package]name="rust-example4"version="0.1.0"edition="2021"#Seemorekeysandtheirdefinitionsathttps://doc.rust-lang.org/cargo/refere......
  • 在Go语言中,你是怎样使用Json的方法
    Encode将一个对象编码成JSON数据,接受一个interface{}对象,返回[]byte和error:func Marshal(v interface{}) ([]byte, error)Marshal函数将会递归遍历整个对象,依次按成......
  • json数据转成list后遍历报错
    错误代码:JSONObjectjsonObject=JSONUtil.parseObj(production);Map<String,Object>resultMap=newHashMap<>();resultMap.put("count",jsonObject.get("count"))......
  • 16.1json模块 16.2文件上传 16.3session的高级用法
    16.1json模块#json主要是干嘛的?json非常严格的数据类型,只能用“”,不然会报错,只支持""#把一个东西变成序列#[1,2,3,4,5]#有序的叫序列#{"a",'b'}#散列#importjson......
  • 包和模块、库、json库
    七.包和模块package:针对代码结构的组织,一个包里面可以拥有很多的python文件module:一个python文件就是一个模块1.从另一个模块直接引用所需代码模块必须是存放在......
  • [记]Rust可变引用与不可变引用
    --->fnmain(){letmutvar=0_i32;{letp1=&mutvar;//p1指针本身不能被重新绑定,我们可以通过p1改变变量var的值*p1=1;pr......
  • 2022-09-07 wx.getLocation暂未配置在app.json且没有权限
    前言:昨晚提交了小程序审核,当即就被驳回了,原因是:wx.getLocation接口无权限,且没有在app.json中配置注:我的是wepy小程序解决方案:找到app.wpy,在里面添加代码如下:required......