-
Notifications
You must be signed in to change notification settings - Fork 0
/
stdjson.go
51 lines (45 loc) · 989 Bytes
/
stdjson.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package json
import (
"bufio"
"encoding/json"
"fmt"
"io"
"os"
"strings"
)
/*
File name : stdjson.go
Author : miaoyc
Create date : 2021/12/28 11:27 上午
Description :
*/
// {"category": "b", "value": 6}
type TestStruct struct {
Category string `json:"category"`
Value int `json:"value"`
}
func parseStdJson(fileName string) {
var testStruct TestStruct
f, _ := os.Open(fileName)
defer f.Close()
buf := bufio.NewReader(f)
for {
line, err := buf.ReadString('\n')
if err != nil || io.EOF == err {
line = strings.TrimSpace(line)
fmt.Println(testStruct.Category, testStruct.Value)
err = json.Unmarshal([]byte(line), &testStruct)
break
}
line = strings.TrimSpace(line)
err = json.Unmarshal([]byte(line), &testStruct)
fmt.Println(testStruct.Category, testStruct.Value)
}
}
func genJson() {
var testStruct TestStruct
testStruct.Category = "a"
testStruct.Value = 1000
line, _ := json.Marshal(testStruct)
fmt.Println(string(line))
}