在 serde 中将多个 JSON 字段反序列化为单个 Vec

Deserializing multiple JSON fields to a single Vec in serde

你能帮我解决以下问题吗?

我有一个 JSON:

{
    "some_other_field": "value",
    "option1": "value1",
    "option2": "value2",
    [...]
    "option10": "value10",
}

最多有 X optionX 个字段都是可选的

我想将其反序列化为具有单个 Vec<String> 的结构,其中向量中的索引对应于字段名称中的后缀:

options[1] -> value of `option1`
options[10] -> value of `option10`

这是我的结构:

struct Data {
    some_other_field: String,
    options: Vec<String>,
}

我尝试了很多东西,包括编写自定义解串器,但我无法通过这个问题:(

任何帮助将不胜感激。

解决此问题的一种方法是使用 serde_json 序列化 JSON 数据,然后使用临时映射来选取这些值。我跳过了很多错误检查,但这是大纲:

use serde_json::{Result, Value};
use std::collections::HashMap;

#[derive(Debug)]
struct Data {
    some_other_field: String,
    options: Vec<String>,
}

fn text_to_data(text: &str) -> Result<Data> {
    let prefix: &'static str = "option";

    // Deserialize JSON
    let value: Value = serde_json::from_str(text)?;
    let value_map = value.as_object().unwrap();

    // Filter keys for "option*", cut prefix, convert to integer and store it in a map
    let options_map: HashMap<usize, &str> = value_map.iter()
        .filter(|it| it.0.starts_with(prefix))
        .map(|it| (it.0[prefix.len()..].parse::<usize>().unwrap(), it.1.as_str().unwrap()))
        .collect();

    // Get the maximum of options
    let options_count = options_map.iter().map(|it| it.0).max().unwrap();

    // Collect values to a vector or use empty string as default
    let options: Vec<String> = (0..=*options_count)
        .map(|it| options_map.get(&it).unwrap_or(&"").to_string()).collect();

    // Also access other fields in JSON
    let some_other_field =
        value.get("some_other_field").unwrap().as_str().unwrap().to_string();

    Ok(Data { some_other_field, options })
}


fn main() {
    let data = r#"
        {
            "some_other_field": "value",
            "option1": "value1",
            "option2": "value2",
            "option10": "value10"
        }"#;

    let x = text_to_data(data);
    println!("{:?}", x);
}

输出:

Ok(Data { some_other_field: "value", options: ["", "value1", "value2", "", "", "", "", "", "", "", "value10"] })

您可以通过遍历已解析的值来构造一个字符串数组,然后将其传递给您的父结构。

use serde_json::{Result, Value};

fn main() -> Result<()> {
    let data = r#"
        {
          "some_other_field": "value",
          "option1": "value1",
          "option2": "value2",
          "option8": "value8",
          "option10": "value10"
        }"#;

    let v: Value = serde_json::from_str(data)?;

    let mut k = v
        .as_object()
        .unwrap()
        .iter()
        .filter(|(k, val)| k.contains("option") && val.as_str() != None)
        .map(|(k, vs)| {
            (
                k.replace("option", "").parse::<usize>().unwrap(),
                vs.as_str().unwrap().to_string(),
            )
        })
        .collect::<Vec<(usize, String)>>();

    k.sort();
    let mut result: Vec<String> = vec![];
    let mut it: usize = 0;
    let mut idx: usize = 0;
    let mut tidx: usize = 0;
    while idx < k.len() {
        tidx = k[idx].0;
        while it != tidx {
            result.push("".to_string());
            it += 1;
        }
        result.push(k[idx].to_owned().1);
        idx += 1;
        it += 1;
    }
    println!("{:?}", result);
    Ok(())
}

输出:

["", "value1", "value2", "", "", "", "", "", "value8", "", "value10"]