gpt4 book ai didi

json - 从 API 结果解析 JSON

转载 作者:行者123 更新时间:2023-11-29 08:30:47 25 4
gpt4 key购买 nike

我正在使用 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",
)

我想解析此输出并仅打印我从调用中收到的第一个数组(下面 BOLD 中的数字)中的“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);
}

上面打印出我想要的数字并且工作得很好,但是当我在“响应”的其余部分中发表评论时我无法让它工作。

求助! :)

最佳答案

您的字符串作为一个整体不是有效的 JSON,因为它包含多个对象。您可以使用 StreamDeserializer 绕过它一个一个地提取对象:

for value in Deserializer::from_str (data).into_iter::<Value>() {
println!("value: {:?}", value.unwrap());
}

Playground

关于json - 从 API 结果解析 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58534591/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com