首页 > 其他分享 >实例-rust-从json文件中读取数据并将输出写入json文件

实例-rust-从json文件中读取数据并将输出写入json文件

时间:2022-09-18 00:02:26浏览次数:83  
标签:文件 use 读取数据 serde Write json Output Data

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>>  {

    /////从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)?;



    /////将结果写入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(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,读取数据,serde,Write,json,Output,Data
From: https://www.cnblogs.com/Nazorine/p/16704004.html

相关文章

  • delphi 解决SaveDialog保存文件时,因无后缀名产生的错误
    vardefaultPath:string;//这里可以设置为全局变量beginifSaveDialog1.ExecutethenbegindefaultPath:=SaveDialog1.FileName;//文件路径+文件名......
  • 自动生成logstash导入配置文件
    Controllerpackagediit.microservice.midrange.controller;importcom.github.xiaoymin.knife4j.annotations.ApiOperationSupport;importdiit.microservice.midrang......
  • 【Python小工具】文件解压
    文件解压#-*-coding:utf-8-*-importosimportsysimporttimeimportshutilimportfiletypeimportgzipimporttarfileimportzipfileclassFileUtils:......
  • 在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"))......
  • 共享库文件缺失
    共享库文件缺失报错Stoppingkeepalived:[FAILED]Startingkeepalived:/export/servers/keepalived-1.2.13/sbin/keepalived:......
  • sh /bin/bash^M: 坏的解释器: 没有那个文件或目录
    原因:由于windows系统下换行符为 \r\n,linux下换行符为 \n,所以导致在windows下编写的文件会比linux下多回车符号 \r。解决方案:[root@localhost~]#sed's/\r//'-i......
  • IDEA提交代码 忽略target目录下的文件
     1.File->Settings->FileTypes在IgnoreFilesandFolders一栏后面添加:target;settings;.project;.iml;.log;.logs;.classpath;.factorypath; ......
  • 配置文件yaml和ini
    前言本文主要介绍配置文件yaml和ini的读取。一、yaml文件YAML是一个可读性高,易于理解,用来表达数据序列化的格式。语法与python的语法类似。文件后缀 .yaml下......
  • Linux 文件与目录管理
    目录操作目录切换cd命令用于从当目录切换到目标目录语法:cd切换到的目录特殊变量.表示当前目录-表示切换到当前目录的上一级目录-表示返回到上一此切换的......