栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在golang json.Unmarshal中处理单个或数组结构的好方法是什么?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在golang json.Unmarshal中处理单个或数组结构的好方法是什么?

通过

UnmarshalJSON()
覆盖控制解组过程。

https://play.golang.org/p/pCSgymQYC3

package mainimport (    "log"    "encoding/json"    "bytes")const(    s1=`{"query":{"count":1,"created":"2016-05-11T02:12:33Z","lang":"en-US","results":{"quote":{"Change":"+0.21","DaysLow":"9.32","DaysHigh":"9.68","Name":"Alcoa Inc. Common Stock","Open":"9.56","PreviousClose":"9.46","Symbol":"aa","Volume":"22266533","PercentChange":"+2.22%"}}}}`    s2=`{"query":{"count":2,"created":"2016-05-11T02:17:48Z","lang":"en-us","results":{"quote":[{"Change":"+0.21","DaysLow":"9.32","DaysHigh":"9.68","Name":"Alcoa Inc. Common Stock","Open":"9.56","PreviousClose":"9.46","Symbol":"aa","Volume":"22266533","PercentChange":"+2.22%"},{"Change":"+0.63","DaysLow":"92.11","DaysHigh":"93.57","Name":"Apple Inc.","Open":"93.35","PreviousClose":"92.79","Symbol":"aapl","Volume":"33686836","PercentChange":"+0.68%"}]}}}`)type QueryResult struct {    //Id bson.ObjectId `bson:"_id,omitempty"`    Query Query `json:"query"`}type Query struct {    Count int `json:"count"`    Created string `json:"created"`    Lang string `json:"lang"`    Results Quote `json:"results"`}type structOrArray struct{    parent *Quote    s StockQuote    a []StockQuote}func (this *structOrArray)UnmarshalJSON(data []byte) error{    d := json.NewDeprer(bytes.NewBuffer(data))    t, err := d.Token();    if err != nil{        return err    }    if t==json.Delim('['){        if err := json.Unmarshal(data, &this.a);err != nil { return err        }        return nil    }    if err := json.Unmarshal(data, &this.s);err != nil {        return err    }    return nil}type fakeQuote struct{    Load structOrArray `json:"quote"` //Here is the difference. Do I need to re-write the whole struct in order to handle []}type Quote struct {    Quote *StockQuote    Quotes []StockQuote}func (this *Quote)UnmarshalJSON(data []byte) error{    fq := fakeQuote{}    if err := json.Unmarshal(data, &fq);err != nil{        return err    }    this.Quote = &fq.Load.s    this.Quotes = fq.Load.a    return nil}type StockQuote struct {    Change string `json:"change"`    PercentChange string `json:"percentChange"`    DaysLow string `json:"daysLow"`    DaysHigh string `json:"daysHigh"`    Open string `json:"open"`    PreviousClose string `json:"previousClose"`    Symbol string `json:"symbol"`    Name string `json:"name"`    Volume string `json:"volume"`}func main() {    r := QueryResult{}    err := json.Unmarshal([]byte(s1), &r)    if err != nil {        log.Fatalln(err)    }    log.Println(r.Query.Results.Quote)    log.Println(r.Query.Results.Quotes)    err = json.Unmarshal([]byte(s2), &r)    if err != nil {        log.Fatalln(err)    }    log.Println(r.Query.Results.Quote)    log.Println(r.Query.Results.Quotes)}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/371468.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号