Parsing multiple JSON objects in Go

前端 未结 4 1572
-上瘾入骨i
-上瘾入骨i 2021-01-21 01:55

Objects like the below can be parsed quite easily using the encoding/json package.

[ 
    {\"something\":\"foo\"},
    {\"something-else\":\"bar\"}
         


        
4条回答
  •  温柔的废话
    2021-01-21 02:29

    Seems like each line is its own json object.

    You may get away with the following code which will structure this output into correct json:

    package main
    
    import (
        "fmt"
        "strings"
    )
    
    func main() {
        base := `{"trolo":"lolo"}
    {"trolo2":"lolo2"}`
    
        delimited := strings.Replace(base, "\n", ",", -1)
    
        final := "[" + delimited + "]"
        fmt.Println(final)
    }
    

    You should be able to use encoding/json library on final now.

提交回复
热议问题