Go语言-json解析

摘要

本文部分内容来源于网络,个人收集整理,请勿传播

JSON(Javascript Object Notation, JS 对象标记)是一种轻量级的数据交换语言,它基于 ECMAScript 规范的一个子集,采用完全独立于编程语言的文本格式来存储和表示数据。简洁和清晰的层次结构使得 JSON 成为理想的数据交换语言。 易于人阅读和编写,同时也易于机器解析和生成,并有效地提升网络传输效率。

JSON与XML最大的不同在于XML是一个完整的标记语言,而JSON不是。JSON由于比XML更小、更快,更易解析,以及浏览器的內建快速解析支持,使得其更适用于网络数据传输领域。

encoding/json

Marshal解析输出json

针对JSON的输出,我们在定义struct tag的时候需要注意几点:

  • 字段的tag是“-”,那么这个字段不会输出到JSON
  • tag中带有自定义名称,那么这个自定义名称会出现在JSON的字段名中
  • tag中如果带有“omitempty”选项,那么如果该字段值为空,就不会输出到JSON串中
  • 如果字段类型是bool,string,int,int64等,而tag中带有“,string”选项,那么这个字段在输出到JSON的时候会把该字段对应的值转换成JSON字符串
1
func Marshal(v interface{})([]byte, error)

简单应用

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
package main

import (
"encoding/json"
"fmt"
)

type Stu struct {
Name string `json:"name"`
Age int
HIgh bool
sex string
Class *Class `json:"class"`
}

type Class struct {
Name string
Grade int
}

func main() {
//实例化一个数据结构,用于生成json字符串
stu := Stu{
Name: "张三",
Age: 18,
HIgh: true,
sex: "男",
}

//指针变量
cla := new(Class)
cla.Name = "1班"
cla.Grade = 3
stu.Class = cla

//Marshal失败时err!=nil
jsonStu, err := json.Marshal(stu)
if err != nil {
fmt.Println("生成json字符串错误")
}

//jsonStu是[]byte类型,转化成string类型便于查看
fmt.Println(string(jsonStu))
os.Stdout.Write(jsonStu)
}
  • 只要是可导出成员(变量首字母大写),都可以转成json。因成员变量sex是不可导出的,故无法转成json。
  • 如果变量打上了json标签,如Name旁边的 json:"name" ,那么转化成的json key就用该标签“name”,否则取变量名作为key,如“Age”,“HIgh”。
  • bool类型也是可以直接转换为json的value值。Channel, complex 以及函数不能被编码json字符串。当然,循环的数据结构也不行,它会导致marshal陷入死循环。
  • 指针变量,编码时自动转换为它所指向的值,如cla变量。
    (当然,不传指针,Stu struct的成员Class如果换成Class struct类型,效果也是一模一样的。只不过指针更快,且能节省内存空间。)

interface

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
package main

import (
"encoding/json"
"fmt"
"os"
)

type Stu struct {
Name interface{} `json:"name"`
Age interface{}
HIgh interface{}
sex interface{}
Class interface{} `json:"class"`
}

type Class struct {
Name string
Grade int
}

func main() {
//实例化一个数据结构,用于生成json字符串
stu := Stu{
Name: "张三",
Age: 18,
HIgh: true,
sex: "男",
}

//指针变量
cla := new(Class)
cla.Name = "1班"
cla.Grade = 3
stu.Class = cla

//Marshal失败时err!=nil
jsonStu, err := json.Marshal(stu)
if err != nil {
fmt.Println("生成json字符串错误")
}

//jsonStu是[]byte类型,转化成string类型便于查看
fmt.Println(string(jsonStu))
os.Stdout.Write(jsonStu)

}

slice

https://blog.csdn.net/zxy_666/article/details/80173288

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//正确示范

//方式1:只声明,不分配内存
var stus1 []*StuRead

//方式2:分配初始值为0的内存
stus2 := make([]*StuRead,0)

//错误示范
//new()只能实例化一个struct对象,而[]StuRead是切片,不是对象
stus := new([]StuRead)

stu1 := StuRead{成员赋值...}
stu2 := StuRead{成员赋值...}

//由方式1和2创建的切片,都能成功追加数据
//方式2最好分配0长度,append时会自动增长。反之指定初始长度,长度不够时不会自动增长,导致数据丢失
stus1 := appen(stus1,stu1,stu2)
stus2 := appen(stus2,stu1,stu2)

//成功编码
json1,_ := json.Marshal(stus1)
json2,_ := json.Marshal(stus2)

输出JSON数据串

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
// generalJson
package main

import (
"encoding/json"
"fmt"
)

type Server struct {
ServerName string `json:"serverName"`
ServerIP string `json:"serverIP"`
}

type Serverslice struct {
Servers []Server `json:"servers"`
}

func main() {

var s Serverslice
s.Servers = append(s.Servers, Server{ServerName: "Shanghai_VPN", ServerIP: "127.0.0.1"})
s.Servers = append(s.Servers, Server{ServerName: "Beijing_VPN", ServerIP: "127.0.0.2"})

b, err := json.Marshal(s)
if err != nil {
fmt.Println("json err: ", err)
}

fmt.Println(string(b))
}

Marshal函数只有在转换成功的时候才会返回数据,在转换的过程中我们需要注意几点:

  • JSON对象只支持string作为key,所以要编码一个map,那么必须是map[string]T这种类型(T是Go语言中任意的类型)
  • Channel,complex和function是不能被编码成JSON的
  • 嵌套的数据时不能编码的,不然会让JSON编码进入死循环
  • 指针在编码的时候会输出指针指向的内容,而空指针会输出null
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
// jsontest.go
package main

import (
"encoding/json"
"os"
)

type Server struct {
//ID不会导出到JSON
ID int `json:"-"`

//ServerName的值会进行二次JSON编码
ServerName string `json:"serverName"`
ServerName2 string `json:"serverName2, string"`

//如果ServerIP为空,则不输出到JSON中
ServerIP string `json:"serverIP,omitempty"`
Description string `json:"description,string"`
}

func main() {

s := Server{
ID: 3,
ServerName: `Go "1.0"`,
ServerName2: `Go "1.0"`,
ServerIP: ``,
Description: `描述信息`,
}

b, _ := json.Marshal(s)
os.Stdout.Write(b)
}

Unmarshal 解析json

1
func Unmarshal(data [] byte, v interface{}) error

这个函数会把传入的 data 作为一个JSON来进行解析,解析后的数据存储在参数 v 中。这个参数 v 也是任意类型的参数(但一定是一个类型的指针),原因是我们在是以此函数进行JSON 解析的时候,这个函数不知道这个传入参数的具体类型,所以它需要接收所有的类型。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package main

import (
"encoding/json"
"fmt"
)

func main() {
var jsonBlob = []byte(`[
{"Name": "Platypus", "Order": "Monotremata"},
{"Name": "Quoll", "Order": "Dasyuromorphia"}
]`)
type Animal struct {
Name string
Order string
}
var animals []Animal
err := json.Unmarshal(jsonBlob, &animals)
if err != nil {
fmt.Println("error:", err)
}
fmt.Printf("%+v", animals)
}

Encoders and Decoders

1
2
3
4
5
6
7
8
9
func NewDecoder(r io.Reader) *Decoder

type Decoder struct {
// contains filtered or unexported fields
}

type Encoder struct {
// contains filtered or unexported fields
}
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
package main

import (
"encoding/json"
"fmt"
"io"
"log"
"strings"
)

func main() {
const jsonStream = `
{"Name": "Ed", "Text": "Knock knock."}
{"Name": "Sam", "Text": "Who's there?"}
{"Name": "Ed", "Text": "Go fmt."}
{"Name": "Sam", "Text": "Go fmt who?"}
{"Name": "Ed", "Text": "Go fmt yourself!"}
`
type Message struct {
Name, Text string
}
dec := json.NewDecoder(strings.NewReader(jsonStream))
for {
var m Message
if err := dec.Decode(&m); err == io.EOF {
break
} else if err != nil {
log.Fatal(err)
}
fmt.Printf("%s: %s\n", m.Name, m.Text)
}
}

解析到结构体

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
// json.go
package main

import (
"encoding/json"
"fmt"
)

type Server struct {
ServerName string
ServerIP string
}

type Serverslice struct {
Servers []Server
}

func main() {
var s Serverslice
str := `{"servers":[{"serverName":"Shanghai_VPN","serverIP":"127.0.0.1"},
{"serverName":"Beijing_VPN","serverIP":"127.0.0.2"}]}`

json.Unmarshal([]byte(str), &s)
fmt.Println(s)
fmt.Println(s.Servers[0].ServerIP)
}
  • 先定义了与JSON数据对应的结构体,以及与数组对应的slice
  • 字段名对应JSON里面的key
  • 解析:如果key是Foo
    • 在结构体里面查找含有Foo的可导出的struct字段(首字母大写)
    • 其次查找字段名是Foo的导出字段
    • 最后查找类似FOO或者FoO这样的除了首字母之外其他大小写不敏感的导出字段
  • 能够被赋值的字段必须是可导出字段(即首字母大写)
  • 同时JSON解析的时候只会解析能找到的字段,如果找不到的字段会被忽略
  • 这样的一个好处是:当你接收到一个很大的JSON数据结构而你却只想获取其中的部分数据的时候,你只需将你想要的数据对应的字段名大写

解析到interface

我们知道interface{}可以用来存储任意数据类型的对象,这种数据结构正好用于存储解析的未知结构的json数据的结果。

JSON包中采用map[string]interface{}[]interface{}结构来存储任意的JSON对象和数组。Go类型和JSON类型的对应关系如下:

  • bool: JSON booleans
  • float64: JSON numbers
  • string: JSON strings
  • nil: JSON null
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
// tointer.go
package main

import (
"encoding/json"
"fmt"
)

func main() {

b := []byte(`{"Name":"Wednesday", "Age":6, "Parents": [ "Gomez", "Moticia" ]}`)
var f interface{}
err := json.Unmarshal(b, &f)
if err != nil {
fmt.Println(err)
}

m := f.(map[string]interface{})

for k, v := range m {
switch vv := v.(type) {
case string:
fmt.Println(k, "is string", vv)
case int:
fmt.Println(k, "is int", vv)
case []interface{}:
fmt.Println(k, "is an array:")
for i, u := range vv {
fmt.Println(i, u)
}
default:
fmt.Println(k, "is of a type I don't know how to handle")
}
}
}

通过interface{}type assert的配合,我们就可以解析未知结构的JSON函数了

net/http中应用json

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
52
53
54
55
56
57
58
59
60
package main

import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
)

type User struct {
Id string
Balance uint64
}

func main() {
// 通过POST或PUT或PATCH向服务端发送JSON
u := User{Id: "110", Balance: 8}
b := new(bytes.Buffer)
json.NewEncoder(b).Encode(u)
res, _ := http.Post("https://httpbin.org/post", "application/json; charset=utf-8", b)
io.Copy(os.Stdout, res.Body)

// 在服务端解析JSON
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
var u User
if r.Body == nil {
http.Error(w, "Please send a request body", 400)
return
}
err := json.NewDecoder(r.Body).Decode(&u)
if err != nil {
http.Error(w, err.Error(), 400)
return
}
fmt.Println(u.Id)
})
log.Fatal(http.ListenAndServe(":8080", nil))

// 构造服务端要返回的JSON
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
u := User{Id: "US123", Balance: 8}
json.NewEncoder(w).Encode(u)
})
log.Fatal(http.ListenAndServe(":8080", nil))

// 解析服务端返回的JSON
b1 := new(bytes.Buffer)
json.NewEncoder(b).Encode(u)
res1, _ := http.Post("https://httpbin.org/post", "application/json; charset=utf-8", b1)
var body struct {
// httpbin.org sends back key/value pairs, no map[string][]string
Headers map[string]string `json:"headers"`
Origin string `json:"origin"`
}
json.NewDecoder(res1.Body).Decode(&body)
fmt.Println(body)
}

完整例子

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
package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)

func main() {

url := "https://api.xuebaclass.com/xuebaapi/v1/provinces"

req, _ := http.NewRequest("GET", url, nil)

req.Header.Add("accept", "application/json")
req.Header.Add("content-type", "application/json")

res, _ := http.DefaultClient.Do(req)

defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)

type Province struct {
Id int `json:"id"`
Province string `json:"province"`
}
provinces := make([]Province, 0)
err := json.Unmarshal([]byte(body), &provinces)
if err != nil {
fmt.Println("error:", err)
}
fmt.Println(provinces)

}

go-simplejson

bitly公司开源了一个叫做simplejson的包,在处理未知结构体的JSON时相当方便,地址

1
2
3
4
j, err := simplejson.NewJson([]byte(flow))
if err != nil {
fmt.Println("json解析错误")
}

json-iterator/go

解析速度最快的json解析器,它最多能比普通的解析器快 10 倍之多,即使在数据绑定的用法下也有同样的性能优势。

安装

1
go get github.com/json-iterator/go

Marshal结构体解析到json

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
package main

import (
"encoding/json"
"fmt"
"os"

jsoniter "github.com/json-iterator/go"
)

func main() {
type ColorGroup struct {
ID int
Name string
Colors []string
}
group := ColorGroup{
ID: 1,
Name: "Reds",
Colors: []string{"Crimson", "Red", "Ruby", "Maroon"},
}
b, err := json.Marshal(group)
if err != nil {
fmt.Println("error:", err)
}
os.Stdout.Write(b)

var json_iterator = jsoniter.ConfigCompatibleWithStandardLibrary
b, err = json_iterator.Marshal(group)
os.Stdout.Write(b)
}

Unmarshal解析json到结构体

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
package main

import (
"encoding/json"
"fmt"

jsoniter "github.com/json-iterator/go"
)

func main() {
var jsonBlob = []byte(`[
{"Name": "Platypus", "Order": "Monotremata"},
{"Name": "Quoll", "Order": "Dasyuromorphia"}
]`)
type Animal struct {
Name string
Order string
}
var animals []Animal
err := json.Unmarshal(jsonBlob, &animals)
if err != nil {
fmt.Println("error:", err)
}
fmt.Printf("%+v", animals)

var animals2 []Animal
var json_iterator = jsoniter.ConfigCompatibleWithStandardLibrary
json_iterator.Unmarshal(jsonBlob, &animals2)
fmt.Printf("%+v", animals2)
}

jsoniter.Get解析不固定结构json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package main

import (
"fmt"

jsoniter "github.com/json-iterator/go"
)

func main() {

val := []byte(`{"ID":1,"Name":"Reds","Colors":["Crimson","Red","Ruby","Maroon"]}`)
str := jsoniter.Get(val, "Colors", 0).ToString()
fmt.Println(str)
}

NewDecoder

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package main

import (
"fmt"
"strings"

jsoniter "github.com/json-iterator/go"
)

func main() {
json := jsoniter.ConfigCompatibleWithStandardLibrary
reader := strings.NewReader(`{"branch":"beta","change_log":"add the rows{10}","channel":"fros","create_time":"2017-06-13 16:39:08","firmware_list":"","md5":"80dee2bf7305bcf179582088e29fd7b9","note":{"CoreServices":{"md5":"d26975c0a8c7369f70ed699f2855cc2e","package_name":"CoreServices","version_code":"76","version_name":"1.0.76"},"FrDaemon":{"md5":"6b1f0626673200bc2157422cd2103f5d","package_name":"FrDaemon","version_code":"390","version_name":"1.0.390"},"FrGallery":{"md5":"90d767f0f31bcd3c1d27281ec979ba65","package_name":"FrGallery","version_code":"349","version_name":"1.0.349"},"FrLocal":{"md5":"f15a215b2c070a80a01f07bde4f219eb","package_name":"FrLocal","version_code":"791","version_name":"1.0.791"}},"pack_region_urls":{"CN":"https://s3.cn-north-1.amazonaws.com.cn/xxx-os/ttt_xxx_android_1.5.3.344.393.zip","default":"http://192.168.8.78/ttt_xxx_android_1.5.3.344.393.zip","local":"http://192.168.8.78/ttt_xxx_android_1.5.3.344.393.zip"},"pack_version":"1.5.3.344.393","pack_version_code":393,"region":"all","release_flag":0,"revision":62,"size":38966875,"status":3}`)
decoder := json.NewDecoder(reader)
params := make(map[string]interface{})
err := decoder.Decode(&params)
if err != nil {
fmt.Println(err)
} else {
fmt.Printf("%+v\n", params)
}
// 第二层如何解析目前还不清楚
}

对比json

https://www.cnblogs.com/wangzhao765/p/9662331.html

json 文件

1
2
3
4
5
6
7
8
9
10
11
12
13
// json01.json
{
"id":1,
"name":"testjson01",
"isadmin":true
}

// json02.json
{
"isadmin":true,
"name":"testjson01",
"id":1
}

对比

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
package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"reflect"
)

func LoadJson(path string, dist interface{}) (err error) {
var content []byte
if content, err = ioutil.ReadFile(path); err == nil {
err = json.Unmarshal(content, dist)
}
return err
}

func main() {
var (
json1 map[string]interface{}
json2 map[string]interface{}
)
if err := LoadJson("./json01.json", &json1); err != nil {
fmt.Println(err)
}
if err := LoadJson("./json02.json", &json2); err != nil {
fmt.Println(err)
}
fmt.Println(reflect.DeepEqual(json1, json2))
}

细节对比

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package service

import (
"encoding/json"
"fmt"
"io/ioutil"
"reflect"
"strings"
)

type JsonDiff struct {
HasDiff bool
Result string
}

func JsonCompare(left, right map[string]interface{}, n int) (string, bool) {
diff := &JsonDiff{HasDiff: false, Result: ""}
jsonDiffDict(left, right, 1, diff)
if diff.HasDiff {
if n < 0 {
return diff.Result, diff.HasDiff
} else {
return processContext(diff.Result, n), diff.HasDiff
}
}
return "", diff.HasDiff
}

func marshal(j interface{}) string {
value, _ := json.Marshal(j)
return string(value)
}

func jsonDiffDict(json1, json2 map[string]interface{}, depth int, diff *JsonDiff) {
blank := strings.Repeat(" ", (2 * (depth - 1)))
longBlank := strings.Repeat(" ", (2 * (depth)))
diff.Result = diff.Result + "\n" + blank + "{"
for key, value := range json1 {
quotedKey := fmt.Sprintf("\"%s\"", key)
if _, ok := json2[key]; ok {
switch value.(type) {
case map[string]interface{}:
if _, ok2 := json2[key].(map[string]interface{}); !ok2 {
diff.HasDiff = true
diff.Result = diff.Result + "\n-" + blank + quotedKey + ": " + marshal(value) + ","
diff.Result = diff.Result + "\n+" + blank + quotedKey + ": " + marshal(json2[key])
} else {
diff.Result = diff.Result + "\n" + longBlank + quotedKey + ": "
jsonDiffDict(value.(map[string]interface{}), json2[key].(map[string]interface{}), depth+1, diff)
}
case []interface{}:
diff.Result = diff.Result + "\n" + longBlank + quotedKey + ": "
if _, ok2 := json2[key].([]interface{}); !ok2 {
diff.HasDiff = true
diff.Result = diff.Result + "\n-" + blank + quotedKey + ": " + marshal(value) + ","
diff.Result = diff.Result + "\n+" + blank + quotedKey + ": " + marshal(json2[key])
} else {
jsonDiffList(value.([]interface{}), json2[key].([]interface{}), depth+1, diff)
}
default:
if !reflect.DeepEqual(value, json2[key]) {
diff.HasDiff = true
diff.Result = diff.Result + "\n-" + blank + quotedKey + ": " + marshal(value) + ","
diff.Result = diff.Result + "\n+" + blank + quotedKey + ": " + marshal(json2[key])
} else {
diff.Result = diff.Result + "\n" + longBlank + quotedKey + ": " + marshal(value)
}
}
} else {
diff.HasDiff = true
diff.Result = diff.Result + "\n-" + blank + quotedKey + ": " + marshal(value)
}
diff.Result = diff.Result + ","
}
for key, value := range json2 {
if _, ok := json1[key]; !ok {
diff.HasDiff = true
diff.Result = diff.Result + "\n+" + blank + "\"" + key + "\"" + ": " + marshal(value) + ","
}
}
diff.Result = diff.Result + "\n" + blank + "}"
}

func jsonDiffList(json1, json2 []interface{}, depth int, diff *JsonDiff) {
blank := strings.Repeat(" ", (2 * (depth - 1)))
longBlank := strings.Repeat(" ", (2 * (depth)))
diff.Result = diff.Result + "\n" + blank + "["
size := len(json1)
if size > len(json2) {
size = len(json2)
}
for i := 0; i < size; i++ {
switch json1[i].(type) {
case map[string]interface{}:
if _, ok := json2[i].(map[string]interface{}); ok {
jsonDiffDict(json1[i].(map[string]interface{}), json2[i].(map[string]interface{}), depth+1, diff)
} else {
diff.HasDiff = true
diff.Result = diff.Result + "\n-" + blank + marshal(json1[i]) + ","
diff.Result = diff.Result + "\n+" + blank + marshal(json2[i])
}
case []interface{}:
if _, ok2 := json2[i].([]interface{}); !ok2 {
diff.HasDiff = true
diff.Result = diff.Result + "\n-" + blank + marshal(json1[i]) + ","
diff.Result = diff.Result + "\n+" + blank + marshal(json2[i])
} else {
jsonDiffList(json1[i].([]interface{}), json2[i].([]interface{}), depth+1, diff)
}
default:
if !reflect.DeepEqual(json1[i], json2[i]) {
diff.HasDiff = true
diff.Result = diff.Result + "\n-" + blank + marshal(json1[i]) + ","
diff.Result = diff.Result + "\n+" + blank + marshal(json2[i])
} else {
diff.Result = diff.Result + "\n" + longBlank + marshal(json1[i])
}
}
diff.Result = diff.Result + ","
}
for i := size; i < len(json1); i++ {
diff.HasDiff = true
diff.Result = diff.Result + "\n-" + blank + marshal(json1[i])
diff.Result = diff.Result + ","
}
for i := size; i < len(json2); i++ {
diff.HasDiff = true
diff.Result = diff.Result + "\n+" + blank + marshal(json2[i])
diff.Result = diff.Result + ","
}
diff.Result = diff.Result + "\n" + blank + "]"
}

func processContext(diff string, n int) string {
index1 := strings.Index(diff, "\n-")
index2 := strings.Index(diff, "\n+")
begin := 0
end := 0
if index1 >= 0 && index2 >= 0 {
if index1 <= index2 {
begin = index1
} else {
begin = index2
}
} else if index1 >= 0 {
begin = index1
} else if index2 >= 0 {
begin = index2
}
index1 = strings.LastIndex(diff, "\n-")
index2 = strings.LastIndex(diff, "\n+")
if index1 >= 0 && index2 >= 0 {
if index1 <= index2 {
end = index2
} else {
end = index1
}
} else if index1 >= 0 {
end = index1
} else if index2 >= 0 {
end = index2
}
pre := diff[0:begin]
post := diff[end:]
i := 0
l := begin
for i < n && l >= 0 {
i++
l = strings.LastIndex(pre[0:l], "\n")
}
r := 0
j := 0
for j <= n && r >= 0 {
j++
t := strings.Index(post[r:], "\n")
if t >= 0 {
r = r + t + 1
}
}
if r < 0 {
r = len(post)
}
return pre[l+1:] + diff[begin:end] + post[0:r+1]
}

func LoadJson(path string, dist interface{}) (err error) {
var content []byte
if content, err = ioutil.ReadFile(path); err == nil {
err = json.Unmarshal(content, dist)
}
return err
}