本文主要是介绍[bigdata-089]go 以http get从server端读取json然后转化成json格式打印输出,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1. 文档
参考各种文档,不一一列举
2. 代码
package mainimport "fmt"
import "io/ioutil"
import "net/http"
import "encoding/json"func main() {//http getresp, err := http.Get("http://127.0.0.1:8080/v1/shorten/?longurl=http://google.com")if err != nil {// handle error}defer resp.Body.Close()//get bodybody, err := ioutil.ReadAll(resp.Body)//just printfmt.Printf(string(body))fmt.Print("\n")//convert []byte to jsonvar f interface{}json.Unmarshal(body, &f)//convert f to mapm := f.(map[string]interface{})//print v with keyfor k, v := range m {switch vv := v.(type) {case string:fmt.Println(k, "is string", vv)case int:fmt.Println(k, "is int", vv)case []interface{}:fmt.Println(k, "is an array:")for i, u := range vv {fmt.Println(i, u)}default:fmt.Println(k, "is of a type I don't know how to handle")}}
}
这篇关于[bigdata-089]go 以http get从server端读取json然后转化成json格式打印输出的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!