将json反序列化为结构体,并将其赋值到新的结构体中,最后对其新的结构体切片进行排序

栗子:

package main

import (
	"encoding/json"
	"fmt"
	"sort"

	//"sort"
)

type Shards struct {
	Failed     int `json:"failed"`
	Skipped    int `json:"skipped"`
	Successful int `json:"successful"`
	Total      int `json:"total"`
}

type Aggregations struct {
	DocsOverCategoryId DocsOverCategoryId `json:"docs_over_category_id"`
}

type DocsOverCategoryId struct {
	Buckets []Buckets `json:"buckets"`
	//DocCountErrorUpperBound int       `json:"doc_count_error_upper_bound"`
	//SumOtherDocCount        int       `json:"sum_other_doc_count"`
}

//type Buckets struct {
//	CommentNum  interface{} `json:"commentNum"`
//	ContentNum  interface{} `json:"contentNum"`
//	DocCount    int            `json:"doc_count"`
//	InteractNum interface{} `json:"interactNum"`
//	LikeNum     interface{} `json:"likeNum"`
//	ShareNum    interface{} `json:"shareNum"`
//
//}

//type Buckets struct {
//	CommentNum  CommentNum     `json:"commentNum"`
//	ContentNum  map[string]int `json:"contentNum"`
//	DocCount    int            `json:"doc_count"`
//	InteractNum map[string]int `json:"interactNum"`
//	LikeNum     map[string]int `json:"likeNum"`
//	ShareNum    map[string]int `json:"shareNum"`
//}

type Buckets struct {
	CommentNum  CommentNums     `json:"commentNum"`
	ContentNum  CommentNums `json:"contentNum"`
	DocCount    int            `json:"doc_count"`
	InteractNum CommentNums `json:"interactNum"`
	LikeNum     CommentNums `json:"likeNum"`
	ShareNum    CommentNums `json:"shareNum"`
}

type CommentNums struct {
	Value int `json:"value"`
}

type Hits struct {
	Hits     interface{} `json:"hits"`
	MaxScore interface{} `json:"max_score"`
	Total    Total       `json:"total"`
}

type Total struct {
	Relation string `json:"relation"`
	Value    int    `json:"value"`
}

type DataJson struct {
	//Shards       Shards       `json:"_shards"`
	Aggregations Aggregations `json:"aggregations"`
	//Hits         Hits         `json:"hits"`
	//Msg          string       `json:"msg"`
	//Ret          int          `json:"ret"`
	//TimedOut     bool         `json:"timed_out"`
	//Took         int          `json:"took"`
}

//var commentSort, contentSort, interactSort,CommentNums  []int
//for _, _buckets := range str.Aggregations.DocsOverCategoryId.Buckets {
//	commentSort = append(commentSort, _buckets.CommentNum["value"])
//	contentSort = append(contentSort, _buckets.ContentNum["value"])
//	interactSort = append(interactSort, _buckets.InteractNum["value"])
//	//CommentNumd 的类型为map类型:map["string"]int 取出CommentNum 的value的方式为 :CommentNum["value"]
//	CommentNum := _buckets.CommentNum["value"]
//	CommentNums = append(CommentNums,CommentNum)
//
//	fmt.Printf("CommentNum:%+v\n",CommentNum)
//}
//fmt.Printf("CommentNums %+v\n",CommentNums )
//sort.Ints(commentSort)
//sort.Ints(contentSort)
//sort.Ints(interactSort)
//fmt.Println("commentSort: ", commentSort)
//fmt.Println("contentSort: ", contentSort)
//fmt.Println("interactSort: ", interactSort)
//var Buckets []Buckets
//Buckets = append(Buckets,str.Aggregations.DocsOverCategoryId.Buckets...)
type Rank struct {
	CommentNum int `json:"comment_nums"`
	ContentNum int `json:"content_num"`
	DocCount int `json:"doc_counts"`
	InteractNum int `json:"interact_nums"`
	LikeNum int `json:"like_nums"`
	ShareNum int `json:"share_nums"`
}

type Ranks []Rank

func (p Ranks) Len() int           { return len(p) }
func (p Ranks) Less(i, j int) bool { return p[i].CommentNum < p[j].CommentNum }
func (p Ranks) Swap(i, j int)      { p[i], p[j] = p[j], p[i] }


func main() {
	dataJson := `{
  "_shards": {
    "failed": 0,
    "skipped": 0,
    "successful": 10,
    "total": 10
  },
  "aggregations": {
    "docs_over_category_id": {
      "buckets": [
        {
          "commentNum": {
            "value": 795
          },
          "contentNum": {
            "value": 387
          },
          "doc_count": 387,
          "interactNum": {
            "value": 1590
          },
          "key": 22,
          "likeNum": {
            "value": 788
          },
          "shareNum": {
            "value": 7
          }
        },
        {
          "commentNum": {
            "value": 1035
          },
          "contentNum": {
            "value": 349
          },
          "doc_count": 349,
          "interactNum": {
            "value": 2005
          },
          "key": 502,
          "likeNum": {
            "value": 949
          },
          "shareNum": {
            "value": 21
          }
        },
        {
          "commentNum": {
            "value": 6
          },
          "contentNum": {
            "value": 5
          },
          "doc_count": 5,
          "interactNum": {
            "value": 9
          },
          "key": 21,
          "likeNum": {
            "value": 3
          },
          "shareNum": {
            "value": 0
          }
        }
      ],
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0
    }
  },
  "hits": {
    "hits": [],
    "max_score": null,
    "total": {
      "relation": "eq",
      "value": 741
    }
  },
  "msg": "success",
  "ret": 0,
  "timed_out": false,
  "took": 11
}`

	var str DataJson
	json.Unmarshal([]byte(dataJson), &str)
	fmt.Printf("%+v\n", len(str.Aggregations.DocsOverCategoryId.Buckets))

	var ran Rank
//var rans [] Rank
// 此处打印之所以会打印出0 0 0(零值) 是因为是在定义的长度即切片容量的接触进行append(追加的)...
//	var rks = make(Ranks,len(str.Aggregations.DocsOverCategoryId.Buckets))
// 所以要把长度和容量定义为0 或只把长度定义为0
//	var rks = make(Ranks,0,len(str.Aggregations.DocsOverCategoryId.Buckets))
	var rks = make(Ranks,0)
	for _,value :=range str.Aggregations.DocsOverCategoryId.Buckets{
		ran.CommentNum = value.CommentNum.Value
		ran.InteractNum =value.InteractNum.Value
		ran.LikeNum = value.LikeNum.Value
		ran.ShareNum = value.ShareNum.Value
		ran.DocCount =value.DocCount
		ran.ContentNum =value.ContentNum.Value

		rks =append(rks,ran)
	}

	sort.Sort(sort.Reverse(rks))


	fmt.Printf("rks%+v",rks)


}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值