【问题标题】:Can't find solution for rust error " Error("EOF while parsing an object", line: 1, column: 1)"Can\'t find solution for rust error \" Error(\"EOF while parsing an object\", line: 1, column: 1)\"
【发布时间】:2022-08-19 19:50:08
【问题描述】:
use std::fs::File;
use std::io::{prelude::*, BufReader};
use serde_json::{Value};

use mysql::*;
use mysql::prelude::*;

struct Show {
    title: String,
    show_poster: String,
    show_url: String,
}

fn main() {
    // Open and read the file lines
    let path = r\"C:\\Users\\egete\\Desktop\\projects\\cdeneme\\rust\\mysqljson\\src\\info.json\";
    let file = File::open(path).expect(\"Unable to read file\");
    let reader = BufReader::new(file);
    let lines: Vec<String> = reader.lines().map(|l| l.expect(\"Could not parse line\")).collect();

    // Connect to your my-sql database
    let url = Opts::from_url(\"mysql://root:Egetelli99@localhost:3306/jsonmysql\").unwrap();
    let pool = Pool::new(url).unwrap();
    let mut conn = pool.get_conn().unwrap();

    // Loop through the lines
    for line in lines {

        // Parse the line into a JSON object serde_json::Value
        let v: Value = serde_json::from_str(&line).expect(\"Unable to parse\");

        // Since we want to save the streaming services of the said show, we need to 
        // loop an array inside the JSON file.
        let l = v[\"watchAvailability\"][0][\"directUrls\"].as_array().unwrap().len();
        for n in 0..l {
            let streaming_url = v[\"watchAvailability\"][0][\"directUrls\"][n].as_str().clone();
            match streaming_url {
                Some(url) => {
                    // Display the streaming url.  I have to do this to remove the Some(url) warning. 
                    // Unused variables in Rust emits warnings
                    println!(\"{:?}\", url);

                    // Create a vector (array of object).  
                    // This provides you the ability to process multiple objects upon saving
                    let shows = vec![
                        Show { 
                            title: v[\"title\"].as_str().as_deref().unwrap_or(\"Error\").to_string(),
                            show_poster: v[\"posterPath\"].as_str().as_deref().unwrap_or(\"Error\").to_string(),
                            show_url: v[\"watchAvailability\"][0][\"directUrls\"][n].as_str().as_deref().unwrap_or(\"Error\").to_string(),
                        },
                    ];  
                    //Execute an insert query
                    conn.exec_batch(
                        r\"INSERT INTO `shows` (`title`, `show_poster`, `show_url`)
                        VALUES (:title, :show_poster, :show_url)\",
                    shows.iter().map(|s| params! {
                        \"title\" => s.title.clone(),
                        \"show_poster\" => s.show_poster.clone(),
                        \"show_url\" => s.show_url.clone(),
                        })
                    ).unwrap_err();
                },
                _ => println!(\"Error\"),
            }
        }

    }
}

那里有什么问题? 出现此错误: 线程 \'main\' 在 \'Unable to parse: Error(\"EOF while parsing an object\", line: 1, column: 1)\', src\\main.rs:30:52 处惊慌失措 注意:使用RUST_BACKTRACE=1 环境变量运行以显示回溯 错误:进程没有成功退出:target\\debug\\mysqljson.exe(退出代码:101)

  • 为什么要逐行解析 json 文件?看起来info.json 中有一个空行是空的。
  • 它是可流式 JSON 的常用格式,其中每一行都是单独的记录。 jsonlines.org 但是,我同意它可能只是一个空行。

标签: mysql json rust


【解决方案1】:

你正在阅读一个文件。但是如果不完全匹配 json 格式,serde 无法弄清楚如何序列化文件上的数据。

此外,您可能知道您正在使用的操作系统系统,因为 \r\n\n 的差异可能会让您迷失方向。

【讨论】:

    猜你喜欢
    • 2021-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多