我想创建一个包含多种类型的JSON对象.这是结构:
{
"key1": "value",
"key2": ["val", "val", "val"]
"key3": { "keyX": 12 }
}
Run Code Online (Sandbox Code Playgroud)
如何制作HashMap接受所有这些类型的?
我正在尝试这个:
let item = HashMap::new();
item.insert("key1", someString); //type is &str
item.insert("key2", someVecOfStrings); //type is Vec<String>
item.insert("key3", someOtherHashMap); //Type is HashMap<&str, u32>
let response = json::encode(&item).unwrap();
Run Code Online (Sandbox Code Playgroud)
我知道哈希映射没有足够的类型信息,但我不确定如何使其工作.我已经尝试设置一个显式类型item,HashMap<&str, Encodable>但它只是另一个错误.这样做的正确方法是什么?
Pav*_*hov 12
您应该使用枚举类型作为您的值HashMap.该枚举需要为每种可能的类型(布尔值,数字,字符串,列表,映射...)和每个变量的相应类型的关联值提供变量:
enum JsonValue<'a> {
String(&'a str),
VecOfString(Vec<String>),
AnotherHashMap(HashMap<&'a str, u32>),
}
Run Code Online (Sandbox Code Playgroud)
幸运的是,已经存在JSON值类型的实现,这是serde_json crate的一部分,它构建在serde crate上.
如果您使用serde_json包,您的代码将如何显示:
extern crate serde_json;
use serde_json::{Value, Map, Number};
fn main() {
let mut inner_map = Map::new();
inner_map.insert("x".to_string(), Value::Number(Number::from(10u64)));
inner_map.insert("y".to_string(), Value::Number(Number::from(20u64)));
let mut map = Map::new();
map.insert("key1".to_string(), Value::String("test".to_string()));
map.insert(
"key2".to_string(),
Value::Array(vec![
Value::String("a".to_string()),
Value::String("b".to_string()),
]),
);
map.insert("key3".to_string(), Value::Object(inner_map));
println!("{}", serde_json::to_string(&map).unwrap());
// => {"key1":"test","key2":["a","b"],"key3":{"x":10,"y":20}}
}
Run Code Online (Sandbox Code Playgroud)
dto*_*nay 11
这是另一种可能更适合您的方法。该serde_json板条箱提供了一种从 JSON 文本构造serde_json::Value对象的方法。你的例子看起来像这样:
use serde_json::json;
fn main() {
let item = json!({
"key1": "value",
"key2": ["val", "val", "val"],
"key3": { "keyX": 12 }
});
let response = serde_json::to_string(&item).unwrap();
}
Run Code Online (Sandbox Code Playgroud)