...
1,
//wrong,别忘了 开头的 {"xxx": 和结尾的 }
ss=[]byte(`[{"a":"a","b":"b"},{"a":"aa","b":"bb"}]`)
//right
js1 := []byte(`{"huzh":[{"a":"a","b":"b"},{"a":"aa","b":"bb"}]}`)
type S1 struct {
Aa string `json:"a"`
}
type S2 struct {
Huzh []S1 `json:"huzh"`
}
2, 更牛逼的动态解析去这里
https://eagain.net/articles/go-dynamic-json/
3.合并以前的
package huzhm import ( "bytes" "encoding/json" "fmt" "io/ioutil" "net/http" ) type Result struct { Token string `json:"token"` UiVersion string `json:"ui_version"` } type Body struct { Jsonrpc string `json:"jsonrpc"` Result Result Id int `json:"id"` } func GenUrl() (string){ url := "http://1.1.13.10:9000/minio/webrpc" fmt.Println("URL:>", url) var jsonStr = []byte(`{"id":1,"jsonrpc":"2.0","params":{"username":"admin","password":"admin123"}, "method":"Web.Login"}`) req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr)) req.Header.Set("User-Agent", "Mozilla") req.Header.Set("Content-Type", "application/json") client := &http.Client{} resp, err := client.Do(req) if err != nil { panic(err) } defer resp.Body.Close() fmt.Println("response Status:", resp.Status) fmt.Println("response Headers:", resp.Header) body, _ := ioutil.ReadAll(resp.Body) fmt.Println("response Body:", string(body)) ss := string(body) fmt.Println(ss) var r Body err = json.Unmarshal([]byte(ss), &r) if err != nil { fmt.Printf("err was %v", err) } url1 := "http://1.1.13.10:9000/minio/download/huzh/L1/big.txt?token=" + r.Result.Token return url1 }
4, 其他例子
func FM4() { var xx interface{} if err:=json.Unmarshal([]byte(`[["a1","b1","c1"],["a1","b1","c1"]]`),&xx); err != nil{ log.Println("happend") return } log.Println("xx===>",xx.([]interface{})) for k,v := range xx.([]interface{}){ log.Println(k,v.([]interface{})) li:=v.([]interface{}) for k1,v1 :=range li{ log.Println("l2===>",k1,v1.(string)) } } } func FM5() { var xx interface{} s1 := `[[ ["a","b"], ["a","b"], ["a","b"] ], [ ["a","b"], ["a","b"], ["a","b"] ] ]` err := json.Unmarshal([]byte(s1), &xx) if err != nil { return } for _,v := range xx.([]interface{}){ tx:=v.([]interface{}) //log.Println(k,v.([]interface{})) for k1,v1 := range tx{ log.Println("k1v1",k1,v1) txx:=v1.([]interface{}) for k2,v2:=range txx{ log.Println("k2,v2===>",k2,v2.(string)) } } } }