我正在編寫一個API,該API對OSMR服務(wù)器執(zhí)行查詢并檢索一些數(shù)據(jù),嘗試將此數(shù)據(jù)轉(zhuǎn)換為JSON并在我的API中返回它,一切工作幾乎正常,但是結(jié)果字符串中的數(shù)值不是數(shù)值,而是用字符“{}”替換。我不知道發(fā)生了什么,我已經(jīng)研究了這個問題一個小時package mainimport ( "encoding/json" "fmt" "net/http" "net/url" "time" "log" "bytes" "github.com/stretchr/objx" "github.com/go-martini/martini")func PrettyJson(data interface{}) (string) { buffer := new(bytes.Buffer) encoder := json.NewEncoder(buffer) encoder.SetIndent("", " ") err := encoder.Encode(data) if err != nil { return "" } return buffer.String()}func main() { app := martini.Classic() app.Get("/", func() string { return "/" }) app.Get("/routes", func(req *http.Request, res http.ResponseWriter) string { query, err := url.ParseQuery(req.URL.RawQuery) if err != nil { log.Println(err) } var client = &http.Client{Timeout: 100 * time.Second} if v1, v2 := query["src"], query["dst"]; v1 != nil && v2 != nil { var routes_array []map[string]interface{} for _, destination := range query["dst"] { request_url := fmt.Sprintf("http://router.project-osrm.org/route/v1/driving/%s;%s?overview=false", query["src"][0], destination) response, err := client.Get(request_url) if err != nil { log.Fatal(err) return "{\"message\": \"internal server error\"}" } buf := new(bytes.Buffer) buf.ReadFrom(response.Body) newStr := buf.String() document, _ := objx.FromJSON(newStr) route := map[string]interface{}{ "destination": destination, "distance": document.Get("routes[0].distance"), "duration": document.Get("routes[0].duration"), } routes_array = append(routes_array, route) } }
1 回答

函數(shù)式編程
TA貢獻1807條經(jīng)驗 獲得超9個贊
- 1 回答
- 0 關(guān)注
- 116 瀏覽
添加回答
舉報
0/150
提交
取消