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 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用