4个版本
0.2.1 | 2023年9月4日 |
---|---|
0.1.2 | 2023年9月4日 |
0.1.1 | 2023年8月8日 |
0.1.0 | 2023年8月8日 |
#1610 在 数据结构
66KB
1K SLoC
Json Node
以节点树形式处理JSON的一种方法。
安装
导航到您的项目文件夹并运行
cargo add json-node
现在已设置
使用
使用JsonNode
类型将JSON解析为节点。有三种类型的节点:Object
、Array
和JsonValue
。一个Value
可以有多种不同类型的值:String
、Integer
、Float
、Boolean
、Null
。这些都可以通过模式匹配(使用match
语句)或使用if let
语句)来手动查找所需的内容。或者,您可以通过调用into_iter
函数来迭代节点下的每个值。
use json_node::{JsonNode, JsonValueType};
let json = "[1, 2, 3, 4, 5]";
let tree = JsonNode::parse(json).unwrap();
match tree {
JsonNode::Array(arr) => {
for element in arr.iter() {
println!("{:?}", element);
}
}
_ => println!("Expected node to be an Array.")
}
或者
use json_node::{JsonNode, JsonValueType};
let json = "[1, 2, 3, 4, 5]";
let tree = JsonNode::parse(json).unwrap();
for value in tree.into_iter() {
match value {
JsonNode::Value(value) => {
match value {
JsonValueType::Integer(num) => println!("{num}");
_ => println!("Expected integer value.")
}
}
_ => println!("Expected value.")
}
}
它们都会输出
1
2
3
4
5
如果您需要,也可以使用此API将节点构建为JSON字符串。虽然它不会格式化得很好。
开发
如果您想帮助开发此crate,请克隆仓库
git clone https://github.com/Skyppex/json-node.git
拉取请求
欢迎提交拉取请求 :D