How to check if JSON is contain in an array JSON objects in golang

huangapple go评论143阅读模式
英文:

How to check if JSON is contain in an array JSON objects in golang

问题

我有一个JSON对象数组,结构如下:

结构体:

type Data struct {
	TaiList []Tai `json:"taiList"`
}

type Tai struct {
	PlmnId *PlmnId `json:"plmnId"`

	Tac string `json:"tac"`

	Nid string `json:"nid"`
}

type PlmnId struct {
	Mcc string `json:"mcc"`

	Mnc string `json:"mnc"`
}

在MongoDB中的JSON格式如下:

[
{
	"taiList": [
          {
			"plmnId": {
				"mcc": "244",
				"mnc": "24"
			},
			"tac": "00001",
			"nid": "99"
		},
		{
			"plmnId": {
				"mcc": "244",
				"mnc": "34"
			},
			"tac": "00001",
			"nid": "555"
		}
	]
}, 
{
	"taiList": [
       {
		"plmnId": {
			"mcc": "244",
			"mnc": "24"
		},
		"tac": "00001",
		"nid": "99"
	  }
    ]
}
]

在从MongoDB解组后,"taiList"形成了JSON对象。以下是从MongoDB中查找数据的函数,错误检查被故意省略了。

func MongoFind(id string) (Data, error) {

	filter := bson.M{"_id": id}
	var (
		result model.Data

	)
	...
	DB.Collection(tailistCollection).FindOne(context.TODO(),filter).Decode(&result)
	...
	return result, nil
}

MongoFind函数返回Data和错误。我想检查JSON对象"ta"是否包含在"taiList"中。

var ta = model.Tai{
	PlmnId: &model.PlmnId{Mcc: "244", Mnc: "34"},
	Tac:    "00001",
	Nid:    "555",
}

我尝试了下面的代码:

var data Data

func CheckTai(ta models.Tai, data Data) bool {
	for _, Tai := range data.TaiList {
		if reflect.DeepEqual(Tai, ta) {
			return true
		}
	}
	return false
}

但是CheckTai函数返回false。只有当只有一个与列表匹配的JSON对象时,CheckTai才返回true,如下所示:

{
      "taiList": [
        {
          "plmnId": {
            "mcc": "244",
            "mnc": "24"
          },
          "tac": "00001",
          "nid": "555"
        }

      ]
    }

需要帮助或者关于如何进行这个检查的想法。

英文:

I have an array of JSON objects as

Structs:

type Data struct {
	TaiList []Tai `json:"taiList"`
}

type Tai struct {
	PlmnId *PlmnId `json:"plmnId"`

	Tac string `json:"tac"`

	Nid string `json:"nid"`
}

type PlmnId struct {
	Mcc string `json:"mcc"`

	Mnc string `json:"mnc"`
}

The JSON in mongodb is of the form


[
{
	"taiList": [
          {
			"plmnId": {
				"mcc": "244",
				"mnc": "24"
			},
			"tac": "00001",
			"nid": "99"
		},
		{
			"plmnId": {
				"mcc": "244",
				"mnc": "34"
			},
			"tac": "00001",
			"nid": "555"
		}
	]
}, 
{
	"taiList": [
       {
		"plmnId": {
			"mcc": "244",
			"mnc": "24"
		},
		"tac": "00001",
		"nid": "99"
	  }
    ]
}
]

The JSON "taiList" is formed after the unmarhsal from mongodb, error check intentionally omitted.

func MongoFind(id string) (Data, error) {

	filter := bson.M{"_id": id}
	var (
		result model.Data

	)
	...
	DB.Collection(tailistCollection).FindOne(context.TODO(),filter).Decode(&result)
	...
	return result, nil
}

The MongoFind returns Data and error. The Data.TaiList array is what i would like to perform the check.

I would like to check if the JSON object "ta" is contain in the "taiList".

 var ta = model.Tai{
	PlmnId: &model.PlmnId{Mcc: "244", Mnc: "34"},
	Tac:    "00001",
	Nid:    "555",
}

I tried with the code below

var data Data

func CheckTai(ta models.Tai, data Data) bool {
	for _, Tai := range data.TaiList {
		if reflect.DeepEqual(Tai, ta) {
			return true
		}
	}
	return false
}

but CheckTai function return false.
The CheckTai only return true when there is only one JSON object that match the list as

{
      "taiList": [
        {
          "plmnId": {
            "mcc": "244",
            "mnc": "24"
          },
          "tac": "00001",
          "nid": "555"
        }

      ]
    }

Need help or idea on how to perform this check.

答案1

得分: 3

根据评论中的建议,我按照以下方式复制了您的场景:

package main

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

type Data struct {
	TaiList []Tai `json:"taiList"`
}

type Tai struct {
	PlmnId *PlmnId `json:"plmnId"`

	Tac string `json:"tac"`

	Nid string `json:"nid"`
}

type PlmnId struct {
	Mcc string `json:"mcc"`

	Mnc string `json:"mnc"`
}

var data = []byte(`{
    "taiList": [
      {
        "plmnId": {
            "mcc": "244",
            "mnc": "24"
        },
        "tac": "00001",
        "nid": "99"
        },
        {
        "plmnId": {
            "mcc": "244",
            "mnc": "34"
        },
        "tac": "00001",
        "nid": "555"
        }
    ]
}
`)

var ta = Tai{
	PlmnId: &PlmnId{Mcc: "244", Mnc: "34"},
	Tac:    "00001",
	Nid:    "555",
}

func CheckTai(ta Tai, data Data) bool {
	for _, Tai := range data.TaiList {
		if reflect.DeepEqual(Tai, ta) {
			return true
		}
	}
	return false
}

func main() {
	var v Data
	err := json.Unmarshal(data, &v)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(CheckTai(ta, v))
}

输出结果:

true
英文:

Based on the suggestions from comments I replicated your scenario as follows:

package main
import (
"encoding/json"
"fmt"
"log"
"reflect"
)
type Data struct {
TaiList []Tai `json:"taiList"`
}
type Tai struct {
PlmnId *PlmnId `json:"plmnId"`
Tac string `json:"tac"`
Nid string `json:"nid"`
}
type PlmnId struct {
Mcc string `json:"mcc"`
Mnc string `json:"mnc"`
}
var data = []byte(`{
"taiList": [
{
"plmnId": {
"mcc": "244",
"mnc": "24"
},
"tac": "00001",
"nid": "99"
},
{
"plmnId": {
"mcc": "244",
"mnc": "34"
},
"tac": "00001",
"nid": "555"
}
]
}
`)
var ta = Tai{
PlmnId: &PlmnId{Mcc: "244", Mnc: "34"},
Tac:    "00001",
Nid:    "555",
}
func CheckTai(ta Tai, data Data) bool {
for _, Tai := range data.TaiList {
if reflect.DeepEqual(Tai, ta) {
return true
}
}
return false
}
func main() {
var v Data
err := json.Unmarshal(data, &v)
if err != nil {
log.Fatal(err)
}
fmt.Println(CheckTai(ta, v))
}

Output:

true

huangapple
  • 本文由 发表于 2021年5月24日 23:45:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/67675044.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定