嗨,我正在尝试在S3上使用kinesis firehose。我试图读取那些s3文件。我正在使用GO进行阅读。
但是,我无法解析JSON,因为这些值只是追加而没有任何定界符。
这是文件的示例(请注意,原始输入是彼此追加的,为了格式化目的,我用换行符将它们分割开了):
{"ticker_symbol":"PLM","sector":"FINANCIAL","change":-0.16,"price":19.99}
{"ticker_symbol":"AZL","sector":"HEALTHCARE","change":-0.78,"price":16.51}
{"ticker_symbol":"IOP","sector":"TECHNOLOGY","change":-1.98,"price":121.88}
{"ticker_symbol":"VVY","sector":"HEALTHCARE","change":-0.56,"price":47.62}
{"ticker_symbol":"BFH","sector":"RETAIL","change":0.74,"price":16.61}
{"ticker_symbol":"WAS","sector":"RETAIL","change":-0.6,"price":16.72}
我的问题是,如何在Go中解析它?我能想到的一种解决方案是将它们除以}{
,然后再次附加它们。但这确实有点黑。
运动学的软管是否提供分隔符?
------更新------
目前,我已经实现了该解决方案,将所有}{
替换为},{
,然后在开头添加[
,在结尾添加]
。然后解析它。
但是我仍在寻找替代方法,因为此解决方案会限制json对象内容中的任何}{
答案 0 :(得分:2)
创建一个简单的结构以解组分批传入的json。因此,每个批处理json都将解组到json对象中。然后创建一个切片结构,将解析后的json附加到切片中。这会将结果json附加在结构片中。
package main
import (
"encoding/json"
"fmt"
)
type Ticker struct {
TickerSymbol string `json:"ticker_symbol"`
Sector string `json:"sector"`
Change float64 `json:"change"`
Price float64 `json:"price"`
}
var jsonBytes = []byte(`{"ticker_symbol":"PLM","sector":"FINANCIAL","change":-0.16,"price":19.99}`)
func main() {
var singleResult Ticker
var result []Ticker
if err := json.Unmarshal(jsonBytes, &singleResult); err != nil {
fmt.Println(err)
}
if len(result) == 0 {
result = append(result, singleResult)
}
fmt.Printf("%+v", result)
}
已编辑:
如果数据要成批包含彼此附加的json对象,则可以使用正则表达式将}
替换为},
,然后最右边修剪,
对象的有效json数组为:
package main
import (
"fmt"
"regexp"
"strings"
)
type Ticker struct {
TickerSymbol string `json:"ticker_symbol"`
Sector string `json:"sector"`
Change float64 `json:"change"`
Price float64 `json:"price"`
}
var str = `{"ticker_symbol":"PLM","sector":"FINANCIAL","change":-0.16,"price":19.99}
{"ticker_symbol":"AZL","sector":"HEALTHCARE","change":-0.78,"price":16.51}
{"ticker_symbol":"IOP","sector":"TECHNOLOGY","change":-1.98,"price":121.88}
{"ticker_symbol":"VVY","sector":"HEALTHCARE","change":-0.56,"price":47.62}
{"ticker_symbol":"BFH","sector":"RETAIL","change":0.74,"price":16.61}
{"ticker_symbol":"WAS","sector":"RETAIL","change":-0.6,"price":16.72}`
func main() {
r := regexp.MustCompile("}")
output := strings.TrimRight(r.ReplaceAllString(str, "},"), ",")
output = fmt.Sprintf("[%s]", output)
fmt.Println(output)
}
使用r := regexp.MustCompile("}")
将帮助您不必担心}{
之间的空格会干扰替换字符串。因此,只需将}
替换为},
,然后向右修剪。
我使用MustCompile的原因是:
使用正则表达式创建常量时,可以使用 MustCompile编译的变体。普通编译不适用于 常量,因为它有2个返回值。
在Go playground上使用json解析的完整工作代码