Send Json with Rocket [Solved]


#1

Hi :slight_smile: I need help once again with Json management.

I’m trying to send a list of Json as response from my rocket server, the output should look like as follows:

   [
    {"target": "red","location": [[622, 1502352659111],[555,1502352679000],[378, 1502352699000],[365, 1502352709000]]}
    {"target": "blue","location": [[622, 1502352659111],[555,1502352679000],[378, 1502352699000],[365, 1502352709000]]}
   ]

At the moment I’m cheating in this way but it is not even close to an acceptable solution. Do you have any tips?

   #[derive(Serialize, Debug)]
    struct Output {
        target: String,
        location: ??
    }


    #[post("/query")]
    fn querypost() -> String {
       let output = "[{
                 \"target\": \"red\",
                  \"location\": [ 
                                 [622, 1502352659111],
                                 [555, 1502352679000],
                                 [378, 1502352699000],
                                 [365, 1502352709000]]}]";
    format!("{}", output)
 }

#2

Check out the Sending JSON section of Rocket docs. Maybe something like this:

#[derive(Serialize)]
struct Output {
    target: String,
    location: Vec<(u32, u64)>,
}


#[post("/query")]
fn querypost() -> Json<Vec<Output>> {
    Json(vec![
        Output {
            target: "red".to_owned(),
            location: vec![
                (622, 1502352659111),
                (555, 1502352679000),
                (378, 1502352699000),
                (365, 1502352709000),
            ],
        },
        Output {
            target: "blue".to_owned(),
            location: vec![
                (622, 1502352659111),
                (555, 1502352679000),
                (378, 1502352699000),
                (365, 1502352709000),
            ],
        },
    ])
}

#3

It works …thank you a lot!!!
I was trying Vec<Json> with no goo result :S
thank you again