JSON 响应的反序列化在字符串中保留引号
Deserialization of JSON reponse keeps quotation marks in Strings
我正在使用 reqwest 查询 Google API:
let request_url = format!(
"https://maps.googleapis.com/maps/api/place/findplacefromtext/json?input=*\
&inputtype=textquery\
&fields=formatted_address,name,place_id,types\
&locationbias=circle:50@{},{}\
&key=my-secret-key",
lat, lng
);
let mut response = reqwest::get(&request_url).expect("pffff");
let gr: GoogleResponse = response.json::<GoogleResponse>().expect("geeez");
GoogleResponse
结构定义为
#[derive(Debug, Serialize, Deserialize)]
pub struct DebugLog {
pub line: Vec<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Candidate {
pub formatted_address: String,
pub name: String,
pub place_id: String,
pub types: Vec<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct GoogleResponse {
pub candidates: Vec<Candidate>,
pub debug_log: DebugLog,
pub status: String,
}
这一切都可以编译,我可以发出请求,但是我在 String
字段中得到的结果包含原始 "
。应该是这样吗?
例如,打印其中一个格式化地址时,我得到:
"result": "\"Street blabh blahab blahb\"",
当我真正想要的时候
"result": "Street blabh blahab blahb",
我是做错了什么还是这是预期的行为?
我会尝试在这里提供一个简单的例子。
extern crate serde; // 1.0.80
extern crate serde_json; // 1.0.33
use serde_json::Value;
const JSON: &str = r#"{
"name": "John Doe",
"age": 43
}"#;
fn main() {
let v: Value = serde_json::from_str(JSON).unwrap();
println!("{} is {} years old", v["name"], v["age"]);
}
会导致
"John Doe" is 43 years old
原因是,v["name"]
不是 String
,而是 Value
(您可以通过添加行 let a: () = v["name"];
来检查,这将导致错误:expected (), found enum 'serde_json::Value'
).
如果你想要&str
/String
,你必须先用Value::as_str
转换它。
如果相应地更改 println!
行:
println!("{} is {} years old", v["name"].as_str().unwrap(), v["age"]);
它将打印出:
John Doe is 43 years old
我正在使用 reqwest 查询 Google API:
let request_url = format!(
"https://maps.googleapis.com/maps/api/place/findplacefromtext/json?input=*\
&inputtype=textquery\
&fields=formatted_address,name,place_id,types\
&locationbias=circle:50@{},{}\
&key=my-secret-key",
lat, lng
);
let mut response = reqwest::get(&request_url).expect("pffff");
let gr: GoogleResponse = response.json::<GoogleResponse>().expect("geeez");
GoogleResponse
结构定义为
#[derive(Debug, Serialize, Deserialize)]
pub struct DebugLog {
pub line: Vec<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Candidate {
pub formatted_address: String,
pub name: String,
pub place_id: String,
pub types: Vec<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct GoogleResponse {
pub candidates: Vec<Candidate>,
pub debug_log: DebugLog,
pub status: String,
}
这一切都可以编译,我可以发出请求,但是我在 String
字段中得到的结果包含原始 "
。应该是这样吗?
例如,打印其中一个格式化地址时,我得到:
"result": "\"Street blabh blahab blahb\"",
当我真正想要的时候
"result": "Street blabh blahab blahb",
我是做错了什么还是这是预期的行为?
我会尝试在这里提供一个简单的例子。
extern crate serde; // 1.0.80
extern crate serde_json; // 1.0.33
use serde_json::Value;
const JSON: &str = r#"{
"name": "John Doe",
"age": 43
}"#;
fn main() {
let v: Value = serde_json::from_str(JSON).unwrap();
println!("{} is {} years old", v["name"], v["age"]);
}
会导致
"John Doe" is 43 years old
原因是,v["name"]
不是 String
,而是 Value
(您可以通过添加行 let a: () = v["name"];
来检查,这将导致错误:expected (), found enum 'serde_json::Value'
).
如果你想要&str
/String
,你必须先用Value::as_str
转换它。
如果相应地更改 println!
行:
println!("{} is {} years old", v["name"].as_str().unwrap(), v["age"]);
它将打印出:
John Doe is 43 years old