Golang中如何比较两个JSON数据?

Golang中如何比较两个JSON数据? 我正在处理JSON数据。我想知道是否有任何库可以帮助我比较JSON。

示例:

JSON 1:

[{"Mcast":"xyz","Source":"10.11.1.33"},{"Cmts":"pqr","Source":"10.11.1.33"},{"Rpd":"11:30:10:11:11:a1","sinks":""},{"Mcast":"xyz","Source":"10.11.1.32"}]

JSON 2:

[{"Rpd":"11:30:10:11:11:a1","sinks":""},{"Mcast":"xyz","Source":"10.11.1.33"},{"Cmts":"pqr","Source":"10.11.1.33"},{"Mcast":"xyz","Source":"10.11.1.32"}]

这两个JSON是相同的,只是对象的索引顺序不同。


更多关于Golang中如何比较两个JSON数据?的实战教程也可以访问 https://www.itying.com/category-94-b0.html

2 回复

不,JSON 是不同的,因为列表的顺序很重要。

如果你的相等性定义不同,你需要自己实现。

在 Go 语言伪代码中,大致如下所示:

func is_permutation([]map[string]string a, b) bool {
  for _, v := range a {
    if is_member(v, b) {
      delete_element(v, b)
    } else {
      return false
    }
  }
  return true
}

更多关于Golang中如何比较两个JSON数据?的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


在Go中比较两个JSON数据,特别是当顺序不重要时,可以使用github.com/google/go-cmp/cmp库。它提供了灵活的深度比较功能,包括忽略切片顺序的选项。

首先安装库:

go get github.com/google/go-cmp/cmp

示例代码:

package main

import (
    "encoding/json"
    "fmt"
    "github.com/google/go-cmp/cmp"
    "github.com/google/go-cmp/cmp/cmpopts"
)

func main() {
    json1 := `[{"Mcast":"xyz","Source":"10.11.1.33"},{"Cmts":"pqr","Source":"10.11.1.33"},{"Rpd":"11:30:10:11:11:a1","sinks":""},{"Mcast":"xyz","Source":"10.11.1.32"}]`
    json2 := `[{"Rpd":"11:30:10:11:11:a1","sinks":""},{"Mcast":"xyz","Source":"10.11.1.33"},{"Cmts":"pqr","Source":"10.11.1.33"},{"Mcast":"xyz","Source":"10.11.1.32"}]`

    var data1, data2 []map[string]interface{}
    
    json.Unmarshal([]byte(json1), &data1)
    json.Unmarshal([]byte(json2), &data2)

    // 使用cmp比较,忽略切片顺序
    if cmp.Equal(data1, data2, cmpopts.SortSlices(func(a, b map[string]interface{}) bool {
        // 创建一个简单的排序函数
        strA, _ := json.Marshal(a)
        strB, _ := json.Marshal(b)
        return string(strA) < string(strB)
    })) {
        fmt.Println("JSON数据相同")
    } else {
        fmt.Println("JSON数据不同")
    }
}

另一种方法是使用reflect.DeepEqual,但它对顺序敏感:

package main

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

func main() {
    json1 := `[{"Mcast":"xyz","Source":"10.11.1.33"},{"Cmts":"pqr","Source":"10.11.1.33"},{"Rpd":"11:30:10:11:11:a1","sinks":""},{"Mcast":"xyz","Source":"10.11.1.32"}]`
    json2 := `[{"Rpd":"11:30:10:11:11:a1","sinks":""},{"Mcast":"xyz","Source":"10.11.1.33"},{"Cmts":"pqr","Source":"10.11.1.33"},{"Mcast":"xyz","Source":"10.11.1.32"}]`

    var data1, data2 []map[string]interface{}
    
    json.Unmarshal([]byte(json1), &data1)
    json.Unmarshal([]byte(json2), &data2)

    // reflect.DeepEqual对顺序敏感
    if reflect.DeepEqual(data1, data2) {
        fmt.Println("JSON数据相同")
    } else {
        fmt.Println("JSON数据不同")
    }
}

对于简单的JSON比较,也可以手动排序后比较:

package main

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

func sortJSON(data []map[string]interface{}) []map[string]interface{} {
    sort.Slice(data, func(i, j int) bool {
        strI, _ := json.Marshal(data[i])
        strJ, _ := json.Marshal(data[j])
        return string(strI) < string(strJ)
    })
    return data
}

func main() {
    json1 := `[{"Mcast":"xyz","Source":"10.11.1.33"},{"Cmts":"pqr","Source":"10.11.1.33"},{"Rpd":"11:30:10:11:11:a1","sinks":""},{"Mcast":"xyz","Source":"10.11.1.32"}]`
    json2 := `[{"Rpd":"11:30:10:11:11:a1","sinks":""},{"Mcast":"xyz","Source":"10.11.1.33"},{"Cmts":"pqr","Source":"10.11.1.33"},{"Mcast":"xyz","Source":"10.11.1.32"}]`

    var data1, data2 []map[string]interface{}
    
    json.Unmarshal([]byte(json1), &data1)
    json.Unmarshal([]byte(json2), &data2)

    sorted1 := sortJSON(data1)
    sorted2 := sortJSON(data2)

    sortedJSON1, _ := json.Marshal(sorted1)
    sortedJSON2, _ := json.Marshal(sorted2)

    if string(sortedJSON1) == string(sortedJSON2) {
        fmt.Println("JSON数据相同")
    } else {
        fmt.Println("JSON数据不同")
    }
}

go-cmp库提供了最灵活的解决方案,特别是当需要忽略字段或处理复杂嵌套结构时。

回到顶部