我使用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”值:
好({“结算日期”:“2019-10-24T12:50:00Z”,“注册ID”:“VIC1”,“RRP.DISPATCHPRICE0”:68.0464}\n
...
我对生锈还很陌生,我想知道怎样才能最好地做到这一点。
社区里有人能帮忙提些建议/帮助吗。
我试过什么:
我一直在使用以下代码:
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());
}
Playground