第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在golang json.Unmarshal中處理單個或數(shù)組結構的好方法是什么?

在golang json.Unmarshal中處理單個或數(shù)組結構的好方法是什么?

Go
BIG陽 2022-01-10 14:53:59
我正在使用 Go 和 Yahoo API 構建一個股票報價網(wǎng)絡應用程序。問題是如何在不編寫另一個結構的情況下在數(shù)組和單個結構之間切換。我不知道如何用語言來解釋。這是示例:從 Yahoo API 獲取一個符號報價如下所示:{"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%"}}}}從 Yahoo API 獲取多個報價:{"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%"}]}}}不同的是引號變成了一個數(shù)組[]。使用時如何處理json.Unmarshal(quoteResultRawJSON, &queryResult)?我的結構看起來像: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 Quote struct {  Quote StockQuote `json:"quote"` //Here is the difference. Do I need to re-write the whole struct in order to handle []}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"` }我是否必須編寫另一個結構來處理數(shù)組?
查看完整描述

1 回答

?
jeck貓

TA貢獻1909條經(jīng)驗 獲得超7個贊

使用UnmarshalJSON()覆蓋控制解組過程。


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


package main


import (

    "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.NewDecoder(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)

}


查看完整回答
反對 回復 2022-01-10
  • 1 回答
  • 0 關注
  • 322 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號