Closed tymur999 closed 3 months ago
ChatGPT recommends this alternative:
let deserialized: Value = serde_json::from_str(json_str).unwrap();
// Convert the serde_json::Value into a vector of strings
if let Value::Array(array) = deserialized {
let string_array: Vec<String> = array.into_iter()
.filter_map(|v| v.as_str().map(|s| s.to_string()))
.collect();
// Print the vector of strings
println!("{:?}", string_array);
}
}
But as you can see, this is very unwieldy
['hello','two']
is not valid JSON.
Originally this was stored in an env variable as ["hello","two"] which was for some reason parsed as [hello, two] which explains the err.
When I escape the " as \"
, it parses fine.
It can parse a root array of objects but not a root array of strings.
Expects a value after key 'hello' (but this is not a key, it is a string array)