GO处理一些未知结构的JSON,纠结一个星期了

vus520 · · 3877 次点击
```go type Mydate struct { Took int32 `json:"took"` Timed_out bool `json:"timed_out"` T_shards struct { Total int32 `json:"total"` Successful int32 `json:"successful"` Failed int32 `json:"failed"` } `json:"_shards"` Aggregations struct { User struct { Doc_count_error_upper_bound int32 `json:"doc_count_error_upper_bound"` Sum_other_doc_count int32 `json:"sum_other_doc_count"` Buckets []Mydate4 `json:"buckets"` } `json:"user"` } `json:"aggregations"` } type Mydate4 struct { Key string `json:"key"` Doc_count int32 `json:"doc_count"` Tags struct { Doc_count_error_upper_bound int32 `json:"doc_count_error_upper_bound"` Sum_other_doc_count int32 `json:"sum_other_doc_count"` Buckets []Mydate6 `json:"buckets"` } `json:"tags"` } type Mydate6 struct { Key string `json:"key"` Doc_count int32 `json:"doc_count"` Ct struct { Value int32 `json:"value"` } `json:"ct"` } var s = `{"took":2,"timed_out":false,"_shards":{"total":5,"successful":3,"failed":0},"hits":{"total":22,"max_score":0,"hits":[]},"aggregations":{"user":{"doc_count_error_upper_bound":0,"sum_other_doc_count":0,"buckets":[{"key":"olivere","doc_count":16,"tags":{"doc_count_error_upper_bound":0,"sum_other_doc_count":0,"buckets":[{"key":"golang","doc_count":16,"ct":{"value":16}},{"key":"elasticsearch","doc_count":7,"ct":{"value":7}}]}},{"key":"sandrae","doc_count":6,"tags":{"doc_count_error_upper_bound":0,"sum_other_doc_count":0,"buckets":[{"key":"cycling","doc_count":6,"ct":{"value":6}},{"key":"sports","doc_count":6,"ct":{"value":6}}]}}]}}}` func main() { var r Mydate if err := json.Unmarshal([]byte(s), &r); err != nil { panic(err) } else { fmt.Println("r = %#v", r) } } ``` 楼主试试,记得把中间我没有加上的hits那段加上
#5
更多评论
``` https://github.com/bennyscetbun/jsongo ``` 是否满足你的需求?
#1
谢谢,看了一下,发现demo里只有encode的操作,没有decode的操作
#2