Wreq problem (Error(Json(Error("invalid type: integer `0)

as soon as I try this code :

extern crate reqwest;

use std::collections::HashMap;

fn main() -> Result<(), Box<std::error::Error>> {
    let resp: HashMap<String, String> = reqwest::get("https://www.rijksmuseum.nl/api/nl/collection?key=xxxxxx&format=json&type=schilderij&toppieces=True")?
    println!("{:#?}", resp);

I see this error message

 Finished dev [unoptimized + debuginfo] target(s) in 1m 40s
     Running `target\debug\json-try.exe`
Error: Error(Json(Error("invalid type: integer `0`, expected a string", line: 1, column: 24)))
error: process didn't exit successfully: `target\debug\json-try.exe` (exit code: 1)

I expect this as response

  "elapsedMilliseconds": 164,
  "count": 359,
  "artObjects": [
      "links": {
        "self": "https://www.rijksmuseum.nl/api/nl/collection/SK-C-5",
        "web": "https://www.rijksmuseum.nl/nl/collection/SK-C-5"
      "id": "nl-SK-C-5",
      "objectNumber": "SK-C-5",
      "title": "Schutters van wijk II onder leiding van kapitein Frans Banninck Cocq, bekend als de ‘Nachtwacht’",
      "hasImage": true,
      "principalOrFirstMaker": "Rembrandt Harmensz. van Rijn",
      "longTitle": "Schutters van wijk II onder leiding van kapitein Frans Banninck Cocq, bekend als de ‘Nachtwacht’, Rembrandt Harmensz. van Rijn, 1642",
      "showImage": true,
      "permitDownload": true,
      "webImage": {
        "guid": "92253da1-794d-49f4-9e3c-e4c160715f53",
        "offsetPercentageX": 50,
        "offsetPercentageY": 100,
        "width": 2500,
        "height": 2034,
        "url": "http://lh6.ggpht.com/wwx2vAS9DzFmmyeZefPjMtmCNOdjD80gvkXJcylloy40SiZOhdLHVddEZLBHtymHu53TcvqJLYZfZF7M-uvoMmG_wSI=s0"

It looks like your code is reading the JSON data as HashMap<String, String>. That type means the keys are strings and the values are strings. An example of a HashMap<String, String> in JSON would be:

  "key1": "value1",
  "key2": "value2"

The example response you showed has many values that are not strings, such as 359 which is a number.

oke, any advice on a better data-type

and yes, It can be a string, a number or a nested json structure.

If you do expect that some entity in you program could be one of the several types, this is a good candidate for enum. Reqwest uses serde to deserialize json output, and serde has inherent support for this case (you might like to see "Untagged" at the bottom of page).

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.