使用Golang将复杂字符串解析为JSON。

huangapple go评论83阅读模式
英文:

golang parse a complex string to json

问题

我正在尝试将从服务器返回的字符串响应解析为JSON格式。我对golang还不熟悉,需要一些帮助来理解正确的解决方法。以下是我从服务器获取的响应:

测试1:本地1.1.1.1 远程2.2.2.2 状态良好
测试ID:2.2.2.2
测试类型:ABD
管理状态:启动
DFD:禁用
地址族:ipv4-单播
选项:<刷新>
接收更新:0,发送更新:7
接收数据:853,发送数据:860
自上次接收更新以来的时间:n/a
转换为良好状态的次数:1
自上次进入良好状态以来的时间:22384秒
重试间隔:120秒
保持时间:90秒,测试时间:30秒

测试2:本地1.1.1.1 远程2.2.2.2 状态良好
测试ID:2.2.2.2
测试类型:ABD
管理状态:启动
DFD:禁用
地址族:ipv4-单播
选项:<刷新>
接收更新:0,发送更新:7
接收数据:853,发送数据:860
自上次接收更新以来的时间:n/a
转换为良好状态的次数:1
自上次进入良好状态以来的时间:22384秒
重试间隔:120秒
保持时间:90秒,测试时间:30秒

测试3:本地1.1.1.1 远程2.2.2.2 状态良好
测试ID:2.2.2.2
测试类型:ABD
管理状态:启动
DFD:禁用
地址族:ipv4-单播
选项:<刷新>
接收更新:0,发送更新:7
接收数据:853,发送数据:860
自上次接收更新以来的时间:n/a
转换为良好状态的次数:1
自上次进入良好状态以来的时间:22384秒
重试间隔:120秒
保持时间:90秒,测试时间:30秒

谢谢。

英文:

I am trying to parse a string response from a server to JSON format. I am new to golang and need some help in understanding the right way to achieve a solution. Here is the response I am getting from the server -

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: &lt; Refresh &gt;  
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: &lt; Refresh &gt;  
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: &lt; Refresh &gt;  
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

Thanks.

答案1

得分: 0

你需要编写一个自定义解析器,将输入切割成可以检索键和值的方式。strings 包应该非常有帮助,特别是 strings.Split

我写了一个基本示例,可以在你的输入的至少一个部分上工作。你需要调整它,使其适用于整个输入。目前,我的示例在继续读取时会覆盖键。你需要添加某种数组结构来处理使用相同键的情况。此外,我的示例将所有值都作为字符串使用。我不确定这对你是否有用。

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

然而,如果你控制生成此输出的服务器和应用程序,最好的解决方案是让该应用程序将其转换为 JSON。

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

英文:

You are going to have to write a custom parser that will cut that input up into a way you can retrieve your keys and values. The strings package should be very helpful, specifically strings.Split.

I wrote a basic example that works on at least one section of your input. You are going to want to tweak it to work for your entire input. As it stands, mine will overwrite keys when continuing to read. You will want to add some sort of array structure to handle using the same keys. Also, mine uses all values as strings. I'm not sure if that is useful to you.

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

However, if you control the server and application that you are getting this output from, the preferred solution would be to have that application convert it to JSON.

NOTE: The code above is very brittle.

huangapple
  • 本文由 发表于 2015年5月9日 08:23:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/30134667.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定