gpt4 book ai didi

json - 在 Go 语言中解析 JSON : Attribute without name

转载 作者:IT王子 更新时间:2023-10-29 02:28:58 27 4
gpt4 key购买 nike

我在将 JSON 文件从 API 解析到 Go 时遇到了一些问题,这是我要解析的 JSON:

{"method":"stats.provider.ex",
"result":{
"addr":"17a212wdrvEXWuipCV5gcfxdALfMdhMoqh",
"current":[{
"algo":3, // algorithm number (3 = X11)
"name":"X11", // algorithm name
"suffix":"MH", // speed suffix (kH, MH, GH, TH,...)
"profitability":"0.00045845", // current profitability in BTC/suffix/Day
"data":[{ // speed object can contain following fields:
// a (accepted), rt (rejected target), rs (rejected stale),
// rd (rejected duplicate) and ro (rejected other)
// if fields are not present, speed is 0
"a":"23.09", // accepted speed (in MH/s for X11)
"rs":"0.54", // rejected speed - stale
},
"0.0001234" // balance (unpaid)
]},
... // other algorithms here
],
"past":[{
"algo":3,
"data":[
[4863234, // timestamp; multiply with 300 to get UNIX timestamp
{"a":"28.6"}, // speed object
"0" // balance (unpaid)
],[4863235,{"a":"27.4"},"0.00000345"],
... // next entries with inc. timestamps
]},
... // other algorithms here
],
"payments":[{
"amount":"0.00431400",
"fee":"0.00023000",
"TXID":"txidhere",
"time":1453538732, // UNIX timestamp
"type":0 // payment type (0 for standard NiceHash payment)
},
... // other payments here
]
}
}

您可以在此链接中找到有关 API 的更多信息:https://www.nicehash.com/doc-api

我遇到的问题是数据属性:

    "data":[{ // speed object can contain following fields:
// a (accepted), rt (rejected target), rs (rejected stale),
// rd (rejected duplicate) and ro (rejected other)
// if fields are not present, speed is 0
"a":"23.09", // accepted speed (in MH/s for X11)
"rs":"0.54", // rejected speed - stale
},
"0.0001234" // balance (unpaid)
]},

因为余额(未付)行,因为它没有名称,所以我不知道如何在 go 中执行结构。

最佳答案

似乎这个“数据”对象可以用以下结构类型来描述(假设它的形状与您的示例没有变化):

type Data struct {
Timestamp *int64
Speed *Speed
Balance *float64
}

type Speed struct {
Accepted *float64 `json:"a,string,omitempty"`
RejectedTarget *float64 `json:"rt,string,omitempty"`
RejectedStale *float64 `json:"rs,string,omitempty"`
RejectedDuplicate *float64 `json:"rd,string,omitempty"`
RejectedOther *float64 `json:"ro,string,omitempty"`
}

“Speed”结构具有 JSON 标签,因为该对象非常适合默认的 JSON un/marshaler。

但是,“数据”结构应该实现自定义 json.UnmarshalJSON这样它就可以处理具有不同类型的 JSON 数组的奇怪选择,以序列化其字段。请注意,我下面的示例实现使用了 json.RawMessage type通过允许 JSON 解码器确保正确的 JSON 数组语法并分别存储每个元素的字节来简化事情,以便我们可以根据它们各自的类型和形状对它们进行解码:

// Parse valid JSON arrays as "Data" by assuming one of the following shapes:
// 1: [int64, Speed, string(float64)]
// 2: [Speed, string(float64)]
func (d *Data) UnmarshalJSON(bs []byte) error {

// Ensure that the bytes contains a valid JSON array.
msgs := []json.RawMessage{}
err := json.Unmarshal(bs, &msgs)
if err != nil {
return err
}

// Parse the initial message as "Timestamp" int64, if necessary.
idx := 0
if len(msgs) == 3 {
ts, err := strconv.ParseInt(string(msgs[idx]), 10, 64)
if err != nil {
return err
}
d.Timestamp = &ts
idx++
}

// Parse the mandatory "Speed" struct per usual.
d.Speed = &Speed{}
err = json.Unmarshal(msgs[idx], &d.Speed)
idx++
if err != nil {
return err
}

// Parse the mandatory "Balance" item after trimming quotes.
balance, err := strconv.ParseFloat(string(msgs[idx][1:len(msgs[idx])-1]), 64)
if err != nil {
return err
}
d.Balance = &balance

return nil
}

因此,您可以像这样将有效的、格式正确的 JSON 数组解析为“数据”对象:

jsonstr := `[
[4863234, {"a":"28.6"}, "0" ],
[{"a":"23.09","rs":"0.54"},"0.0001234"]
]`

datas := []Data{}
err := json.Unmarshal([]byte(jsonstr), &datas)
if err != nil {
panic(err)
}
// datas[0] = Data{Timestamp:4863234,Speed{Accepted:28.6},Balance:0}
// datas[1] = Data{Speed{Accepted:23.09,RejectedStale:0.54},Balance:0.0001234}

当然,您还需要实现 json.MarshalJSON如果您想将“数据”对象序列化为 JSON。

关于json - 在 Go 语言中解析 JSON : Attribute without name,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47981438/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com