drasv0904 2015-07-05 13:18
浏览 54
已采纳

Golang:来自URL的Json作为地图

What is the best way to extract json from a url i.e. Rest service from Go? Also it seems most rest client libraries in go force a use of the json.marshall which needs a struct to be used with it.

This doesn't work in the case of unstructured data where you don't fully know what will be coming in. Is there a way to have it all simply come in as a map[string:string]?

  • 写回答

2条回答 默认 最新

  • duanjin9035 2015-07-05 13:28
    关注

    Why not to parse it into map[string]string as this code have to do

    var d map[string]interface{}
    data, err := json.Unmarshal(apiResponse, &d)
    

    You can traverse data in this struct too.

    If you suspect, that api response can be not singular object, but the collection of objects, the interface{} also works for arrays.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?