dsorecdf78171 2015-05-09 00:23
浏览 90
已采纳

golang将复杂的字符串解析为json

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: < 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

Thanks.

  • 写回答

1条回答 默认 最新

  • dsg435665475 2015-05-09 01:01
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料