dongzen2675 2015-05-05 08:18
浏览 94
已采纳

Golang:从文本文件中读取无效的JSON

I have a txt file with the following sample data:

host{
      Entry {
          id: "foo"
      }
       Entry {
          id: "bar"
      }
    }

port{
      Entry {
          id: "lorem"
      }
       Entry {
          id: "ipsum"
      }
    }

It has +300 of those Entry values. I'd like to read the file and extract the id values belonging to the port section. It's not valid JSON so I can't use the json decoder, is there any other way of extracting the values?

  • 写回答

2条回答 默认 最新

  • douqiao6563 2015-05-05 08:37
    关注

    If the structure is the same throughout and all you want is the id values you can do something like this (on the Playground):

    package main
    
    import (
        "fmt"
        "strings"
    )
    
    func main() {
        // This will work only if ids don't have spaces
        fields := strings.Fields(input1)
        for i, field := range fields {
            if field == "id:" {
                fmt.Println("Got an id: ", fields[i+1][1:len(fields[i+1])-1])
            }
        }
        fmt.Println()
    
        // This will extract all strings enclosed in ""
        for i1, i2 := 0, 0;; {
            i := strings.Index(input2[i1:], "\"") // find the first " starting after the last match
            if i > 0 { // if we found one carry on
                i1 = i + 1 + i1 // set the start index to the absolute position in the string
                i2 = strings.Index(input2[i1:], "\"") // find the second "
                fmt.Println(input2[i1 : i1+i2]) // print the string between ""
                i1 += i2 + 1 // set the new starting index to after the last match
            } else { // otherwise we are done
                break
            }
        }
    
    
        // Reading the text line by line and only processing port sections
        parts := []string{"port{", "  Entry {", "      id: \"foo bar\"", "  }", "   Entry {", "      id: \"more foo bar\"", "  }", "}"}        
        isPortSection := false
        for _, part := range parts {
            if string.HasPrefix(part, "port"){
                isPortSection = true
            }
            if string.HasPrefix(part, "host"){
                isPortSection = false
            }
            if isPortSection && strings.HasPrefix(strings.TrimSpace(part),"id:") {
                line := strings.TrimSpace(part)
                fmt.Println(line[5:len(line)-1])
            }
        }
    }
    
    var input1 string = `port{
      Entry {
          id: "foo"
      }
       Entry {
          id: "bar"
      }
    }`
    
    var input2 string = `port{
      Entry {
          id: "foo bar"
      }
       Entry {
          id: "more foo bar"
      }
    }`
    

    Prints:

    Got an id:  foo
    Got an id:  bar
    
    foo bar
    more foo bar
    

    Instead of printing them in the loop you can stick them into a slice or map or do whatever you want/need to. And of course instead of using the string literal you read in the lines from your file.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)