从 API 结果中解析 JSON
Parsing JSON from an API Result
我正在使用 RUST 和 Reqwest 从 API 中调用以下响应:
Shell:
Ok("{\"SETTLEMENTDATE\":\"2019-10-24T12:50:00Z\",\"REGIONID\":\"VIC1\",\"RRP.DISPATCHPRICE0\":68.0464}\n"{\"SETTLEMENTDATE\":\"2019-10-24T12:55:00Z\",\"REGIONID\":\"VIC1\",\"RRP.DISPATCHPRICE0\":67.4408}\n
{\"SETTLEMENTDATE\":\"2019-10-24T13:00:00Z\",\"REGIONID\":\"VIC1\",\"RRP.DISPATCHPRICE0\":87.2918}\n
{\"SETTLEMENTDATE\":\"2019-10-24T13:05:00Z\",\"REGIONID\":\"VIC1\",\"RRP.DISPATCHPRICE0\":98.1406}\n
{\"SETTLEMENTDATE\":\"2019-10-24T13:10:00Z\",\"REGIONID\":\"VIC1\",\"RRP.DISPATCHPRICE0\":87.4789}\n
{\"SETTLEMENTDATE\":\"2019-10-24T13:15:00Z\",\"REGIONID\":\"VIC1\",\"RRP.DISPATCHPRICE0\":90.2434}\n
{\"SETTLEMENTDATE\":\"2019-10-24T13:20:00Z\",\"REGIONID\":\"VIC1\",\"RRP.DISPATCHPRICE0\":89.8617}\n
{\"SETTLEMENTDATE\":\"2019-10-24T13:25:00Z\",\"REGIONID\":\"VIC1\",\"RRP.DISPATCHPRICE0\":97.2639}\n
{\"SETTLEMENTDATE\":\"2019-10-24T13:30:00Z\",\"REGIONID\":\"VIC1\",\"RRP.DISPATCHPRICE0\":100.493}\n
{\"SETTLEMENTDATE\":\"2019-10-24T13:35:00Z\",\"REGIONID\":\"VIC1\",\"RRP.DISPATCHPRICE0\":93.7984}\n
{\"SETTLEMENTDATE\":\"2019-10-24T13:40:00Z\",\"REGIONID\":\"VIC1\",\"RRP.DISPATCHPRICE0\":85.1182}\n
{\"SETTLEMENTDATE\":\"2019-10-24T13:45:00Z\",\"REGIONID\":\"VIC1\",\"RRP.DISPATCHPRICE0\":91.4173}\n\n",
)
我想解析这个输出,只打印第一个数组中的 'RRP.DISPATCHPRICE0' 值(下图 BOLD 中的数字):
好的("{\"SETTLEMENTDATE\":\"2019-10-24T12:50:00Z\",\"REGIONID\":\"VIC1\",\"RRP.DISPATCHPRICE0\" :68.0464}\n"
...
我对 RUST 还是很陌生,正在努力弄清楚我应该如何最好地实现这一目标。
社区中的任何人都可以帮忙 recommendations/help.
我尝试了什么:
我一直在使用以下代码:
extern crate serde_json;
use serde_json::Value;
fn main() {
let data: Value = serde_json::from_str(
// Ok(
"{\"SETTLEMENTDATE\":\"2019-10-24T12:50:00Z\",\"REGIONID\":\"VIC1\",\"RRP.DISPATCHPRICE0\":68.0464}\n"
// {\"SETTLEMENTDATE\":\"2019-10-24T12:55:00Z\",\"REGIONID\":\"VIC1\",\"RRP.DISPATCHPRICE0\":67.4408}\n
// {\"SETTLEMENTDATE\":\"2019-10-24T13:00:00Z\",\"REGIONID\":\"VIC1\",\"RRP.DISPATCHPRICE0\":87.2918}\n
// {\"SETTLEMENTDATE\":\"2019-10-24T13:05:00Z\",\"REGIONID\":\"VIC1\",\"RRP.DISPATCHPRICE0\":98.1406}\n
// {\"SETTLEMENTDATE\":\"2019-10-24T13:10:00Z\",\"REGIONID\":\"VIC1\",\"RRP.DISPATCHPRICE0\":87.4789}\n
// {\"SETTLEMENTDATE\":\"2019-10-24T13:15:00Z\",\"REGIONID\":\"VIC1\",\"RRP.DISPATCHPRICE0\":90.2434}\n
// {\"SETTLEMENTDATE\":\"2019-10-24T13:20:00Z\",\"REGIONID\":\"VIC1\",\"RRP.DISPATCHPRICE0\":89.8617}\n
// {\"SETTLEMENTDATE\":\"2019-10-24T13:25:00Z\",\"REGIONID\":\"VIC1\",\"RRP.DISPATCHPRICE0\":97.2639}\n
// {\"SETTLEMENTDATE\":\"2019-10-24T13:30:00Z\",\"REGIONID\":\"VIC1\",\"RRP.DISPATCHPRICE0\":100.493}\n
// {\"SETTLEMENTDATE\":\"2019-10-24T13:35:00Z\",\"REGIONID\":\"VIC1\",\"RRP.DISPATCHPRICE0\":93.7984}\n
// {\"SETTLEMENTDATE\":\"2019-10-24T13:40:00Z\",\"REGIONID\":\"VIC1\",\"RRP.DISPATCHPRICE0\":85.1182}\n
// {\"SETTLEMENTDATE\":\"2019-10-24T13:45:00Z\",\"REGIONID\":\"VIC1\",\"RRP.DISPATCHPRICE0\":91.4173}\n\n",
// )
)
.unwrap();
println!("data: {:?}", data);
let obj = data.as_object().unwrap();
let RRP = obj.get("RRP.DISPATCHPRICE0").unwrap();
println!("Value: {}", RRP);
}
上面打印出我想要的数字并且工作得很好但是当我在 'Response'.
的其余部分发表评论时我无法让它工作
请帮忙! :)
您的整个字符串无效JSON,因为它包含多个对象。您可以通过使用 StreamDeserializer
一个一个地提取对象来绕过它:
for value in Deserializer::from_str (data).into_iter::<Value>() {
println!("value: {:?}", value.unwrap());
}
我正在使用 RUST 和 Reqwest 从 API 中调用以下响应:
Shell:
Ok("{\"SETTLEMENTDATE\":\"2019-10-24T12:50:00Z\",\"REGIONID\":\"VIC1\",\"RRP.DISPATCHPRICE0\":68.0464}\n"{\"SETTLEMENTDATE\":\"2019-10-24T12:55:00Z\",\"REGIONID\":\"VIC1\",\"RRP.DISPATCHPRICE0\":67.4408}\n
{\"SETTLEMENTDATE\":\"2019-10-24T13:00:00Z\",\"REGIONID\":\"VIC1\",\"RRP.DISPATCHPRICE0\":87.2918}\n
{\"SETTLEMENTDATE\":\"2019-10-24T13:05:00Z\",\"REGIONID\":\"VIC1\",\"RRP.DISPATCHPRICE0\":98.1406}\n
{\"SETTLEMENTDATE\":\"2019-10-24T13:10:00Z\",\"REGIONID\":\"VIC1\",\"RRP.DISPATCHPRICE0\":87.4789}\n
{\"SETTLEMENTDATE\":\"2019-10-24T13:15:00Z\",\"REGIONID\":\"VIC1\",\"RRP.DISPATCHPRICE0\":90.2434}\n
{\"SETTLEMENTDATE\":\"2019-10-24T13:20:00Z\",\"REGIONID\":\"VIC1\",\"RRP.DISPATCHPRICE0\":89.8617}\n
{\"SETTLEMENTDATE\":\"2019-10-24T13:25:00Z\",\"REGIONID\":\"VIC1\",\"RRP.DISPATCHPRICE0\":97.2639}\n
{\"SETTLEMENTDATE\":\"2019-10-24T13:30:00Z\",\"REGIONID\":\"VIC1\",\"RRP.DISPATCHPRICE0\":100.493}\n
{\"SETTLEMENTDATE\":\"2019-10-24T13:35:00Z\",\"REGIONID\":\"VIC1\",\"RRP.DISPATCHPRICE0\":93.7984}\n
{\"SETTLEMENTDATE\":\"2019-10-24T13:40:00Z\",\"REGIONID\":\"VIC1\",\"RRP.DISPATCHPRICE0\":85.1182}\n
{\"SETTLEMENTDATE\":\"2019-10-24T13:45:00Z\",\"REGIONID\":\"VIC1\",\"RRP.DISPATCHPRICE0\":91.4173}\n\n",
)
我想解析这个输出,只打印第一个数组中的 'RRP.DISPATCHPRICE0' 值(下图 BOLD 中的数字):
好的("{\"SETTLEMENTDATE\":\"2019-10-24T12:50:00Z\",\"REGIONID\":\"VIC1\",\"RRP.DISPATCHPRICE0\" :68.0464}\n" ...
我对 RUST 还是很陌生,正在努力弄清楚我应该如何最好地实现这一目标。 社区中的任何人都可以帮忙 recommendations/help.
我尝试了什么:
我一直在使用以下代码:
extern crate serde_json;
use serde_json::Value;
fn main() {
let data: Value = serde_json::from_str(
// Ok(
"{\"SETTLEMENTDATE\":\"2019-10-24T12:50:00Z\",\"REGIONID\":\"VIC1\",\"RRP.DISPATCHPRICE0\":68.0464}\n"
// {\"SETTLEMENTDATE\":\"2019-10-24T12:55:00Z\",\"REGIONID\":\"VIC1\",\"RRP.DISPATCHPRICE0\":67.4408}\n
// {\"SETTLEMENTDATE\":\"2019-10-24T13:00:00Z\",\"REGIONID\":\"VIC1\",\"RRP.DISPATCHPRICE0\":87.2918}\n
// {\"SETTLEMENTDATE\":\"2019-10-24T13:05:00Z\",\"REGIONID\":\"VIC1\",\"RRP.DISPATCHPRICE0\":98.1406}\n
// {\"SETTLEMENTDATE\":\"2019-10-24T13:10:00Z\",\"REGIONID\":\"VIC1\",\"RRP.DISPATCHPRICE0\":87.4789}\n
// {\"SETTLEMENTDATE\":\"2019-10-24T13:15:00Z\",\"REGIONID\":\"VIC1\",\"RRP.DISPATCHPRICE0\":90.2434}\n
// {\"SETTLEMENTDATE\":\"2019-10-24T13:20:00Z\",\"REGIONID\":\"VIC1\",\"RRP.DISPATCHPRICE0\":89.8617}\n
// {\"SETTLEMENTDATE\":\"2019-10-24T13:25:00Z\",\"REGIONID\":\"VIC1\",\"RRP.DISPATCHPRICE0\":97.2639}\n
// {\"SETTLEMENTDATE\":\"2019-10-24T13:30:00Z\",\"REGIONID\":\"VIC1\",\"RRP.DISPATCHPRICE0\":100.493}\n
// {\"SETTLEMENTDATE\":\"2019-10-24T13:35:00Z\",\"REGIONID\":\"VIC1\",\"RRP.DISPATCHPRICE0\":93.7984}\n
// {\"SETTLEMENTDATE\":\"2019-10-24T13:40:00Z\",\"REGIONID\":\"VIC1\",\"RRP.DISPATCHPRICE0\":85.1182}\n
// {\"SETTLEMENTDATE\":\"2019-10-24T13:45:00Z\",\"REGIONID\":\"VIC1\",\"RRP.DISPATCHPRICE0\":91.4173}\n\n",
// )
)
.unwrap();
println!("data: {:?}", data);
let obj = data.as_object().unwrap();
let RRP = obj.get("RRP.DISPATCHPRICE0").unwrap();
println!("Value: {}", RRP);
}
上面打印出我想要的数字并且工作得很好但是当我在 'Response'.
的其余部分发表评论时我无法让它工作请帮忙! :)
您的整个字符串无效JSON,因为它包含多个对象。您可以通过使用 StreamDeserializer
一个一个地提取对象来绕过它:
for value in Deserializer::from_str (data).into_iter::<Value>() {
println!("value: {:?}", value.unwrap());
}