从数组开始反序列化JSON以与Restson一起使用

时间:2019-09-06 21:58:30

标签: rust serde

我有一个结构:

#[derive(Serialize,Deserialize,Debug)]
struct Post {
    #[serde(rename(deserialize = "userId"))]
    user_id: i32,
    id: i32,
    title: String,
    body: String,
}

我需要将JSON反序列化为Vec<Post>

extern crate restson;
extern crate serde;
extern crate serde_derive;

use std::fs;
use std::path::Path;
use restson::{RestClient, RestPath, Error};
use serde_derive::{Serialize, Deserialize};

const URI: &str ="https://jsonplaceholder.typicode.com/";
impl RestPath<()> for Vec<Post> {
    fn get_path(_: ()) -> Result<String, Error> {
        Ok(String::from("posts"))
    }
}

fn main() {
    let mut client = RestClient::new(URI).unwrap();

    let posts: Vec<Post> = client.get(()).unwrap();
    println!("{:?}", posts.len());
}

我是新手,所以请帮助我。

0 个答案:

没有答案