go读取多条json到结构体数组中
使用背景
JSON(JavaScript Object Notation)是一种比XML更轻量级的数据交换格式,在易于人们阅读和编写的同时,也易于程序解析和生成。尽管JSON是JavaScript的一个子集,但JSON采用完全独立于编程语言的文本格式,且表现为key/value的文本描述形式(与GO语言中的map极为相似),这使它成为较理想的、跨平台的、跨语言的数据交换语言。网上很多demo的案例很简单并不能帮助我们处理问题,当需要go按行读取多条json时博客的知识少之又少,我的写博客的目的就是为了补大家所惑。
创建一个json文件
使用vim创建一个.json文件并将其命名为test.json
{"table_type":"l3","cookie":0,"vrf":12,"ip":50374844,"mask":32,"type":"vm_position","vm_mac":"00:16:3e:1d:20:01","gw_mac":"10:22:33:44:15:16","remote_ip":50440384,"vni":20}
{"table_type":"l3","cookie":0,"vrf":12,"ip":50374845,"mask":32,"type":"vm_position","vm_mac":"00:16:3e:1d:20:01","gw_mac":"10:22:33:44:15:16","remote_ip":50440383,"vni":20}
{"table_type":"l3","cookie":0,"vrf":12,"ip":50374846,"mask":32,"type":"vm_position","vm_mac":"00:16:3e:1d:20:01","gw_mac":"10:22:33:44:15:16","remote_ip":50440385,"vni":20}
{"table_type":"l3","cookie":0,"vrf":12,"ip":50374847,"mask":32,"type":"vm_position","vm_mac":"00:16:3e:1d:20:01","gw_mac":"10:22:33:44:15:16","remote_ip":50440386,"vni":20}
demo
package main
import (
"encoding/json"
"fmt"
"log"
"os"
"bufio"
"io"
)
type RouteTableEntryProps struct {
TableType *string `thrift:"table_type,1" db:"table_type" json:"table_type,omitempty"`
TableIndex *int32 `thrift:"table_index,2" db:"table_index" json:"table_index,omitempty"`
Vrf *int32 `thrift:"vrf,4" db:"vrf" json:"vrf,omitempty"`
IP *int32 `thrift:"ip,5" db:"ip" json:"ip,omitempty"`
Mask *int32 `thrift:"mask,7" db:"mask" json:"mask,omitempty"`
Type *string `thrift:"type,8" db:"type" json:"type,omitempty"`
// unused field # 11
VMMac *string `thrift:"vm_mac,12" db:"vm_mac" json:"vm_mac,omitempty"`
GwMac *string `thrift:"gw_mac,13" db:"gw_mac" json:"gw_mac,omitempty"`
RemoteIP *int32 `thrift:"remote_ip,14" db:"remote_ip" json:"remote_ip,omitempty"`
Vni *int32 `thrift:"vni,16" db:"vni" json:"vni,omitempty"`
}
// This structure is responsible for the use of JSON transmission from the client to the server
// Internal structure nested using pointer type
// Easy to parse
type AllProps struct {
//Command line first parameter (for example port or route)
First_Para *string `thrift:"first_para,1" db:"first_para" json:"first_para,omitempty"`
//Command line first parameter (for example add or del)
Second_Para *string `thrift:"second_para,2" db:"second_para" json:"second_para,omitempty"`
RouteTableEntry *RouteTableEntryProps `thrift:"routetableentry,4" db:"routetableentry" json:"routetableentry,omitempty"`
RouteTableEntries []*RouteTableEntryProps `thrift:"routetableentries,4" db:"routetableentries" json:"routetableentries,omitempty"`
}
func main() {
fp, err := os.OpenFile("./test.json", os.O_RDONLY, 0755)
if err != nil {
fmt.Printf("Error: %s\n", err)
return
}
defer fp.Close()
var allprops *AllProps=new(AllProps)
var first_para ="entries"
allprops.First_Para=&first_para
br := bufio.NewReader(fp)
for {
json_message,_, c := br.ReadLine() //按行读取文件
if c == io.EOF {
break
}
var rtep * RouteTableEntryProps=new (RouteTableEntryProps)
err := json.Unmarshal(json_message[:len(json_message)], rtep)
if err != nil {
log.Fatal(err)
}
allprops.RouteTableEntries=append(allprops.RouteTableEntries,rtep)
}
//将其对应进行输出查看是否正确
fmt.Println(allprops)
}
结果如下图
红色结果为对应结构体指针的地址
总结
我们可以导入bufio库来进行按行读取之后,按读取的结果在切片中进行追加