我正在使用 RUST 和 Reqwest 从 API 调用以下响应:
壳:
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 ”值:
好的("{\"SETTLEMENTDATE\":\"2019-10-24T12:50:00Z\",\"REGIONID\":\"VIC1\",\"RRP.DISPATCHPRICE0\": 68.0464 }\n" 。 ..
我对 RUST 仍然很陌生,并试图找出我应该如何最好地实现这一目标。社区中的任何人都可以提供一些建议/帮助。
我尝试了什么:
我一直在使用以下代码:
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);
}
上面打印出我想要的数字并且效果很好,但是当我在“响应”的其余部分发表评论时,我无法让它工作。
请帮忙!:)