0

I am trying to parse a WolframAlpha response JSON into Rust into which I a fairly new at and it has been a pain compared to some other easier languages.

For now i am just trying to print it out, but i get the following error:

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: reqwest::Error { kind: Decode, source: Error("invalid type: map, expected 
a sequence", line: 1, column: 0) }', src\main.rs:10:38

the code is:

use serde::{Deserialize,Serialize};
use reqwest::blocking::get;

fn main() {
    let api = String::from("api_key"); //here i insert my api key
  let request_url = format!("http://api.wolframalpha.com/v2/query?input=2+2&appid={}&includepodid=Result&format=plaintext&output=json",api);
  
  
  let res = get(request_url).unwrap();
  let songs = res.json::<Response>().unwrap();
  
}


pub type Response = Vec<Subpod>;


#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Root {
    pub queryresult: Queryresult,
}

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Queryresult {
    pub success: bool,
    pub error: bool,
    pub numpods: i64,
    pub datatypes: String,
    pub timedout: String,
    pub timedoutpods: String,
    pub timing: f64,
    pub parsetiming: f64,
    pub parsetimedout: bool,
    pub recalculate: String,
    pub id: String,
    pub host: String,
    pub server: String,
    pub related: String,
    pub version: String,
    pub inputstring: String,
    pub pods: Vec<Pod>,
    pub assumptions: Assumptions,
}

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Pod {
    pub title: String,
    pub scanner: String,
    pub id: String,
    pub position: i64,
    pub error: bool,
    pub numsubpods: i64,
    pub primary: bool,
    pub subpods: Vec<Subpod>,
    pub expressiontypes: Expressiontypes,
    pub states: Vec<State>,
}

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Subpod {
    pub title: String,
    pub plaintext: String,
}

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Expressiontypes {
    pub name: String,
}

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct State {
    pub name: String,
    pub input: String,
    pub stepbystep: bool,
}

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Assumptions {
    #[serde(rename = "type")]
    pub type_field: String,
    pub template: String,
    pub count: i64,
    pub values: Vec<Value>,
}

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Value {
    pub name: String,
    pub desc: String,
    pub input: String,
}

    // Access parts of the data by indexing with square brackets


according to this it should work but for the love of me i can't get it to work. The JSON file looks like this:

{
    "queryresult":{
        "success":true,
        "error":false,
        "numpods":1,
        "datatypes":"Math",
        "timedout":"",
        "timedoutpods":"",
        "timing":0.219,
        "parsetiming":7.4e-2,
        "parsetimedout":false,
        "recalculate":"",
        "id":"MSP6890222gb121hfce2h120000209ah8d6c903e02b",
        "host":"https:\/\/www6b3.wolframalpha.com",
        "server":"15",
        "related":"https:\/\/www6b3.wolframalpha.com\/api\/v1\/relatedQueries.jsp?id=MSPa6891222gb121hfce2h120000412450def98cb95e8884920455003441207",
        "version":"2.6",
        "inputstring":"2 2",
        "pods":[
            {
                "title":"Result",
                "scanner":"Simplification",
                "id":"Result",
                "position":100,
                "error":false,
                "numsubpods":1,
                "primary":true,
                "subpods":[
                    {
                        "title":"",
                        "plaintext":"4"
                    }
                ],
                "expressiontypes":{
                    "name":"Default"
                },
                "states":[
                    {
                        "name":"Step-by-step solution",
                        "input":"Result__Step-by-step solution",
                        "stepbystep":true
                    }
                ]
            }
        ],
        "assumptions":{
            "type":"ListOrTimes",
            "template":"Assuming ${desc1}. Use ${desc2} instead",
            "count":2,
            "values":[
                {
                    "name":"Times",
                    "desc":"multiplication",
                    "input":"ListOrTimes_Times"
                },
                {
                    "name":"List",
                    "desc":"a list",
                    "input":"ListOrTimes_List"
                }
            ]
        }
    }
}
mvolaric
  • 11
  • 1

0 Answers0