在Go中过滤json流中的非json内容 [英] Filtering non-json content in a json stream in Go

查看:143
本文介绍了在Go中过滤json流中的非json内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Go中的json结构输入流。我收到来自我stdin上的另一个应用程序的输入流,并且我无法更改通信协议。



我遇到的问题是每个json结构都以非结束-json字符串行:end(不含引号)。

我使用Golang编码器/ json包来解码我从stdin接收的json。问题是解码器在我第二次用msg调用它的时候会产生一个错误:无效字符'e'寻找值的开始。

这个问题,当然是,结束字符串不是json编码。我想知道如何让Go的json解码器跳过这个字符串?



一些示例输入:

 {command:ack,id:1231231} 
end
{command:fail,id: 1231231}
end
{
command:log,
//消息记录
msg:hello world!
}
end

我试过的东西:




  • 我声明:endStr:= make([] byte,10)
  • 我试过使用fmt。 fscanf(os.Stdin,%s,endStr)读取字符串,但没有读取数据。
  • 我尝试使用os.Stdin.Read(endStr ),但它也不返回任何数据。
  • 在读取第一个json结构之后,dec.Buffered()返回一个包含end字符串的io.Reader,但是我没有知道如何告诉解码器跳过这一步。



任何帮助将不胜感激。



    li>丢弃json解码器,
  1. 从标准输入读取一个字节片段,
  2. 修剪片段以排除(\ end \ n )字符串

  3. 将修剪后的片段传递给json Unmarshaller

写:

  //创建一个缓冲区以保存流数据
data:= make([] byte,5000)

//从循环中的stdin读取数据
for {
_,err = os.Stdin.Read(data)
if err!= nil {
panic(err)


index:= bytes.Index(data,[] byte(\\\
))
data = data [:index]

var myStruct MyStruct $ b $ err = json.Unmarshal(data& myStruct)
if err!= nil {
panic(err)
}

/ /(用myStruct做点什么)
}


I'm working with an input stream of json structures in Go. I receive the input stream from another application on my stdin and I can't alter the communications protocol.

The problem I have is that every json structure is terminated by a non-json string line: "end" (without the quotes).

I'm using the Golang encoder/json package to decode the json I'm receiving from stdin. The problem is that the decoder produces an error the second time I call it with the msg: "invalid character 'e' looking for beginning of value".

The issue, of course is, that the "end" string is not json encoded. I would like to know how I can have Go's json decoder skip over this string?

Some sample input:

{"command": "ack", "id": "1231231"}
end
{"command": "fail", "id": "1231231"}
end
{
    "command": "log",
    // the message to log
    "msg": "hello world!"
}
end

Things I've tried:

  • I declared: endStr := make([]byte, 10)
  • I've tried to use fmt.Fscanf(os.Stdin, "%s", endStr), to read past the string, but no data are read.
  • I've tried to use os.Stdin.Read(endStr), but it also returns no data.
  • After I read the first json structure, dec.Buffered() returns an io.Reader containing the "end" string, but I don't know how to tell the decoder to skip over this.

Any help would be appreciated.

解决方案

So the best solution I've been able to come up with is:

  1. Ditch the json Decoders,
  2. read a byte slice from stdin,
  3. trim the slice to exclude the ("\nend\n") character string
  4. pass the trimmed slice to a json Unmarshaller

The code I had to write:

// Create a buffer to hold the stream data
data := make([]byte, 5000)

// Read data from stdin in a loop
for {
    _, err = os.Stdin.Read(data)
    if err != nil {
        panic(err)
    }

    index := bytes.Index(data, []byte("\n"))
    data = data[:index]

    var myStruct MyStruct
    err = json.Unmarshal(data, &myStruct)
    if err != nil {
        panic(err)
    }

    //(Do something with myStruct)
}

这篇关于在Go中过滤json流中的非json内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆