繁体   English   中英

将多个 JSON 字段反序列化为 serde 中的单个 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 数据,然后使用临时 map 来选择这些值。 我跳过了很多错误检查,但这里是大纲:

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);
}

Output:

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(())
}

Output:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM