gpt4 book ai didi

json - golang 解析一个复杂的字符串为json

转载 作者:数据小太阳 更新时间:2023-10-29 03:37:40 26 4
gpt4 key购买 nike

我正在尝试将来自服务器的字符串响应解析为 JSON 格式。我是 golang 的新手,需要一些帮助来理解实现解决方案的正确方法。这是我从服务器得到的响应 -

Test 1: local 1.1.1.1  remote 2.2.2.2  state GOOD
Test ID: 2.2.2.2
Test Type: ABD
Admin State: START
DFD: Disabled
Address family: ipv4-unicast
Options: < Refresh >
Updates Received: 0, Updates Sent: 7
Data Received: 853, Data Sent: 860
Time since last received update: n/a
Number of transitions to GOOD: 1
Time since last entering GOOD state: 22384 seconds
Retry Interval: 120 seconds
Hold Time: 90 seconds, Keep Test Time: 30 seconds

Test 2: local 1.1.1.1 remote 2.2.2.2 state GOOD
Test ID: 2.2.2.2
Test Type: ABD
Admin State: START
DFD: Disabled
Address family: ipv4-unicast
Options: < Refresh >
Updates Received: 0, Updates Sent: 7
Data Received: 853, Data Sent: 860
Time since last received update: n/a
Number of transitions to GOOD: 1
Time since last entering GOOD state: 22384 seconds
Retry Interval: 120 seconds
Hold Time: 90 seconds, Keep Test Time: 30 seconds

Test 3: local 1.1.1.1 remote 2.2.2.2 state GOOD
Test ID: 2.2.2.2
Test Type: ABD
Admin State: START
DFD: Disabled
Address family: ipv4-unicast
Options: < Refresh >
Updates Received: 0, Updates Sent: 7
Data Received: 853, Data Sent: 860
Time since last received update: n/a
Number of transitions to GOOD: 1
Time since last entering GOOD state: 22384 seconds
Retry Interval: 120 seconds
Hold Time: 90 seconds, Keep Test Time: 30 seconds

谢谢。

最佳答案

您将不得不编写一个自定义解析器,将输入分解为您可以检索键和值的方式。 strings包应该很有帮助,特别是strings.Split .

我编写了一个基本示例,它至少适用于您输入的一部分。您将要调整它以适用于您的整个输入。就目前而言,我的将在继续阅读时覆盖 key 。您将需要添加某种数组结构来处理使用相同的键。此外,我的所有值都用作 string。我不确定这对您是否有用。

http://play.golang.org/p/FZ_cQ-b-bx

但是,如果您控制从中获取此输出的服务器和应用程序,则首选解决方案是让该应用程序将其转换为 JSON。

注意:上面的代码非常脆弱。

关于json - golang 解析一个复杂的字符串为json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30134667/

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