鎖定6小時。目前正在解決此問題的內容存在爭議。它目前不接受新的答案或互動。
我有兩個要比較的 JSON 字串。我不知道怎么辦
第一個字串
{"timestamp":"2021-10-26T15:09:35.322019337Z","prices":[{"country":"fr","price":{"recommended":0}},{"country":"at","price":{"recommended":0}},{"country":"de","price":{"recommended":0}}]}
第二個字串
{"timestamp":"2021-10-26T15:09:35.322019337Z","prices":[{"country":"fr","price":{"recommended":0}},{"country":"de","price":{"recommended":0}},{"country":"at","price":{"recommended":0}}]}
如您所見,陣列中有不同的順序。
uj5u.com熱心網友回復:
您可以解組它們,并使用reflect.DeepEqual:
var v1, v2 interface{}
json.Unmarshal([]byte(s1),&v1)
json.Unmarshal([]byte(s2),&v2)
if reflect.DeepEqual(v1,v2) {
// They are the same
} else {
// They are different
}
還有其他 JSON 比較包。
uj5u.com熱心網友回復:
這是比較字串的簡單方法:
if strings.Compare(string1, string2) != 0 {
return errors.New("strings don't match")
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/338478.html
標籤:走
上一篇:GoLang空結構體構建
下一篇:如何回傳golang中的錯誤?
