Rust-使用serde/reqwest进行反序列化;“无效类型”;

Rust-使用serde/reqwest进行反序列化;“无效类型”;,rust,deserialization,json-deserialization,serde,reqwest,Rust,Deserialization,Json Deserialization,Serde,Reqwest,我正在尝试反序列化下面的API响应(为了简单起见,我只复制数组的两个部分,但实际上它会更大)。为了演示示例,代码过于简化 API响应: [[16096320000003218532968348733197518908.902488776],[16095456000029349.8325015432183332922900022012.92431526] 所以它是一个大数组/向量,由带有六个整数或浮点数的数组/向量组成(它们的位置也会变化) 为此,我尝试使用泛型,但似乎我遗漏了一些东西,因为我无法

我正在尝试反序列化下面的API响应(为了简单起见,我只复制数组的两个部分,但实际上它会更大)。为了演示示例,代码过于简化

API响应:

[[16096320000003218532968348733197518908.902488776],[16095456000029349.8325015432183332922900022012.92431526]

所以它是一个大数组/向量,由带有六个整数或浮点数的数组/向量组成(它们的位置也会变化)

为此,我尝试使用泛型,但似乎我遗漏了一些东西,因为我无法编译它

这是失败的

thread'main'在'Err'值上调用'Result::unwrap()'时惊慌失措{kind:Decode,source:Error(“无效类型:整数'1609632000000',预期结构T',行:1,列:15)}

use blocking::Response;
use serde::{Deserialize, Serialize};
use reqwest::{blocking, Error, StatusCode};

struct allData<T> {
    data: Slice<T>
}

#[derive(Debug, Serialize, Deserialize)]
struct Slice<T>{
    data1: T,
    data2: T,
    data3: T,
    data4: T,
    data5: T,
    data6: T,

}


fn get_data() -> Option<Slice> /*I know the signature is not correct, read until the end for the correct one*/ {
    let url = format!("{}", my_url_string);
    let response: Slice<T> = blocking::get(&url).unwrap().json().unwrap();
    Some(response);


}

fn main() {
   let call_the_api = get_data();
   println!("{:?}", call_the_api);
}

切片
结构上的
反序列化
派生在JSON数组上不起作用,而是需要一个JSON dict,其中包含字段
data1
data2
等等。大概,您不想为您的类型手动实现
反序列化
特性,因此您需要一个
Vec
来为嵌套数组建模:

#[derive(Deserialize)]
#[serde(transparent)]
struct AllData<T>(Vec<Vec<T>>);
对于枚举,不再需要
AllData
上的类型参数
T
,您可以选择:

#[derive(Deserialize)]
#[serde(transparent)]
struct AllData(Vec<Vec<FloatOrInt>>);
#[派生(反序列化)]
#[serde(透明)]
结构所有数据(Vec);
如果确定内部数组的长度始终为6,则可以将其替换为数组:
[FloatOrInt;6]

把它放在一起,你会得到这样的结果:

#[derive(Deserialize)]
// note, this causes deserialization to try the variants top-to-bottom
#[serde(untagged)]
enum FloatOrInt {
    Int(i64),
    Float(f64),
}
#[derive(Deserialize)]
#[serde(transparent)]
struct AllData(Vec<Vec<FloatOrInt>>);