在 Python 中,您可以執(zhí)行以下操作:r = requests.get("http://wikidata.org/w/api.php", params=params)data = r.json()現(xiàn)在data是一個字典或哈希表(另外,我不需要事先定義字典的結構),我可以通過做 data["entities"], data["entities"]["Q12 來訪問鍵的值“], 等等。我將如何在 golang 中做到這一點?到目前為止,我有這個:resp, err := http.Get("http://wikidata.org/w/api.php?"+v.Encode())if err != nil { // handle error}defer resp.Body.Close()decoder := json.NewDecoder(resp.Body)var data interface{}decodeErr := decoder.Decode(&data)if decodeErr != nil { // handle error}fmt.Println(data["entities"], data["entities"]["Q"+id])這給了我編譯錯誤: invalid operation: data["entities"] (index of type interface {})那么應該var data是什么類型呢?我是否需要事先為 JSON 定義一個結構,或者是否可以在不修改代碼的情況下處理任何 JSON 文件/流?
- 2 回答
- 0 關注
- 172 瀏覽
添加回答
舉報
0/150
提交
取消