3 回答

TA貢獻1744條經驗 獲得超4個贊
我認為您將需要將unmarshal它們遞歸(如果它們包含嵌套的 json)轉換為類似的內容map[string]interface{},然后循環遍歷并比較鍵。在這個問題上提到了一些庫https://stackoverflow.com/a/42153666可以unmarshal安全地使用它們。
例如,您可以Exists在遍歷未編組映射中的鍵時使用 gabs 庫,以查看其他映射中是否存在相同的鍵。
// From gabs library
// Exists checks whether a field exists within the hierarchy.
func (g *Container) Exists(hierarchy ...string) bool {
return g.Search(hierarchy...) != nil
}
編輯:這里沒有庫:https: //play.golang.org/p/jmfFsLT0G1n基于此代碼高爾夫練習的測試用例:https ://codegolf.stackexchange.com/questions/195476/extract-all-keys-來自對象 json

TA貢獻1802條經驗 獲得超4個贊
Go 的標準庫中提供的 json 包為我們提供了我們需要的所有功能。對于任何 JSON 字符串,解析它的標準方法是:
import "encoding/json" //...
// ... myJsonString := `{"some":"json"}`
// `&myStoredVariable` is the address of the variable we want to store our // parsed data in
json.Unmarshal([]byte(myJsonString), &myStoredVariable)
//...
- 3 回答
- 0 關注
- 225 瀏覽
添加回答
舉報