5
如何使用Go提供JSON响应?
问:目前我打印出我的回应func Index 是这样fmt.Fprintf(w, string(response)) 但是,我怎么能正确地在请求发送JSON,以便它可能由视图消耗? package main import ( "fmt" "github.com/julienschmidt/httprouter" "net/http" "log" "encoding/json" ) type Payload struct { Stuff Data } type Data struct { Fruit Fruits Veggies Vegetables } type Fruits map[string]int type Vegetables map[string]int func Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { response, err := getJsonResponse(); if …