我从 API 返回了一个非常长的 JSON 字符串。
基本上它看起来像这样:
{"data":{.......},"needs_trial": false, "adjust_time": false,"attempts": 1234}
我需要该data
部分内不包含“附录”的所有内容作为字符串。
我尝试在 Go 中解析 JSON 时采用通常的方法:
//fetch the data with http.Get into resp.Body
type MyData struct {
Data map[string]interface{}
}
var data MyData
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
return err
}
我当然得到了正确的部分,但现在显然map[string]interface{}
。我没能将其转换回字符串...
定义Data
字段MyData
asstring
也失败。
我不能直接将其剪掉,因为前缀部分是固定的({"data":
),但尾随部分不是。
我不敢相信我还没能做到这一点,因此在这里问...我已经在这上面花了这么多时间,我可以接受反对票...