英文:
How do I convert `serde_json::Value` into a list or a Vec<_>?
问题
Here is the translated code snippet:
use std::fs::File;
use std::io::Read;
use serde_json::Value;
use crate::scene_builder::SceneBuilder;
fn json_hello_world() {
// 一些 JSON 输入数据作为 &str。也许这来自用户。
let data = r#"
{
"name": "John Doe",
"age": 43,
"phones": [
"+44 1234567",
"+44 2345678"
]
}"#;
// 将数据字符串解析为 serde_json::Value。
let value: Value = serde_json::from_str(data).unwrap();
let phones = &value["phones"];
println!("phone[0]: {}", phones[0]);
//println!("number of phones: {}", phones.size());
// 编译错误:在 `Value` 中找不到方法(.size())
}
fn main() {
json_hello_world();
}
Note: I've removed the HTML entities (e.g., "
) and replaced them with regular double quotes in the translated code.
英文:
So I can read a json file with serde_json
:
use std::fs::File;
use std::io::Read;
use serde_json::Value;
use crate::scene_builder::SceneBuilder;
fn json_hello_world() {
// Some JSON input data as a &str. Maybe this comes from the user.
let data = r#"
{
"name": "John Doe",
"age": 43,
"phones": [
"+44 1234567",
"+44 2345678"
]
}"#;
// Parse the string of data into serde_json::Value.
let value: Value = serde_json::from_str(data).unwrap();
let phones = &value["phones"];
println!("phone[0]: {}", phones[0]);
//println!("number of phones: {}", phones.size());
// compilation error: method (.size()) not found in `Value`
}
fn main() {
json_hello_world();
}
and print out value of a list element with correct index: println!("phone[0]: {}", phones[0]);
But how do I convert such a serde_json::Value
into a list or Vec<Value>
so I can access its size/length, like let phones: Vec<Value> = value["phones"];
or let size = phones.size();
?
答案1
得分: 1
You can use as_array()
:
println!("number of phones: {}", phones.as_array().unwrap().len());
But the right thing to do is to deserialize into a typed struct:
#[derive(serde::Deserialize)]
struct Data {
name: String,
age: u32,
phones: Vec<String>,
}
let value: Data = serde_json::from_str(data).unwrap();
let phones = &value.phones;
println!("phone[0]: {}", phones[0]);
println!("number of phones: {}", phones.len());
英文:
You can use as_array()
:
println!("number of phones: {}", phones.as_array().unwrap().len());
But the right thing to do is to deserialize into a typed struct:
#[derive(serde::Deserialize)]
struct Data {
name: String,
age: u32,
phones: Vec<String>,
}
let value: Data = serde_json::from_str(data).unwrap();
let phones = &value.phones;
println!("phone[0]: {}", phones[0]);
println!("number of phones: {}", phones.len());
答案2
得分: 0
let phones = value["phones"].as_array();
if let Some(phones) = phones {
println!("phone[0]: {}", phones[0]);
let size = phones.len();
println!("number of phones: {}", size);
} else {
println!("Invalid JSON structure: 'phones' is not an array.");
}
英文:
let phones = value["phones"].as_array();
if let Some(phones) = phones {
println!("phone[0]: {}", phones[0]);
let size = phones.len();
println!("number of phones: {}", size);
} else {
println!("Invalid JSON structure: 'phones' is not an array.");
}
The as_array
method returns an Option
because the value may not be an array. In case the value is not an array, the method will return None
, and you can handle that condition accordingly.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论