在go语言中,如何从一个[]byte切片提取json串放入[]byte切片中
{"FileName":"1.txt","ID":"121","IPAdress":"1.1.1.2","ModifyTime":"8:27 2019/9/12","OrgName":"org2"},{"FileName":"2.txt","ID":"121","IPAdress":"1.1.1.2","ModifyTime":"18:06 2020/11/06","OrgName":"org2"}比如这样一个[]byte切片,里面包含了两个json串中间被逗号隔开,请问该怎么快速地提取两个json串,分别放入两个[]byte切片中 用一个简单粗暴的方法
package main
import (
"fmt"
"strings"
)
func main() {
b := []byte(`{"FileName":"1.txt","ID":"121","IPAdress":"1.1.1.2","ModifyTime":"8:27 2019/9/12","OrgName":"org2"},{"FileName":"2.txt","ID":"121","IPAdress":"1.1.1.2","ModifyTime":"18:06 2020/11/06","OrgName":"org2"}`)
s := strings.Split(string(b), "},")
b1 := []byte(s)
b2 := []byte(s)
fmt.Println(b1)
fmt.Println(b2)
}
本帖最后由 野良良良猫 于 2020-11-7 15:28 编辑
彩虹七号 发表于 2020-11-7 09:18
用一个简单粗暴的方法
package main
啊这方法可以,我想再问一嘴,这里有两个json,用split倒是可以,要是有三个呢,比如这样
b := []byte(`{"FileName":"1.txt","ID":"121","IPAdress":"1.1.1.2","ModifyTime":"8:27 2019/9/12","OrgName":"org2"},{"FileName":"2.txt","ID":"121","IPAdress":"1.1.1.2","ModifyTime":"18:06 2020/11/06","OrgName":"org2"},{"FileName":"2.txt","ID":"121","IPAdress":"1.1.1.2","ModifyTime":"18:06 2020/11/06","OrgName":"org2"}`)
因为我这边这个[]byte切片的长度不定,有可能有好几个json 彩虹七号 发表于 2020-11-6 19:30
用一个简单粗暴的方法
package main
好兄弟我悟了,既然两个json串之间的符号是我自己定的,我根本没必要让他是英文逗号,随便放一点json串里面不可能有的符号就可以很好地分开了,非常感谢,不看到你那个中文逗号我都想不到,果然遇事不决问鱼友,好家伙,python板块问go语言都有人帮忙
页:
[1]