如何使用嵌套数组解组 JSON 响应数组

How to unmarshal JSON response array with nested array

我正在尝试解析响应。我是 Go 的新手,我不明白如何为以下响应创建新的类型结构。

这是我需要解析的响应

"result": [["188.68.38.247", "noserver4u.de", ["v1.2", "s50002", "t50001"]], ["37.187.141.73", "electrum.nute.net", ["v1.2", "s50002"]], ["76.174.26.91", "electrum.hsmiths.com", ["v1.2", "s50002", "t50001"]], ["69.27.173.238", "electrum.leblancnet.us", ["v1.2", "s50002", "t50001"]], ["79.219.53.95", "electrum3.hachre.de", ["v1.2", "s50002", "t50001"]], ["178.254.34.161", "v25437.1blu.de", ["v1.2", "s50002"]], ["50.35.67.146", "orannis.com", ["v1.2", "s50002", "t50001"]], ["18.206.2.52", "18.206.2.52", ["v1.2", "s50002", "t50001"]], ["96.27.8.242", "technetium.network", ["v1.2", "s50002"]], ["5.9.124.124", "hetzner01.fischl-online.de", ["v1.2", "s50002"]], ["213.168.187.27", "electrum.petrkr.net", ["v1.2", "s50002", "t50001"]], ["82.217.214.215", "Bitcoin-node.nl", ["v1.1", "s50002", "t50001"]], ["173.91.90.62", "electrumx.bot.nu", ["v1.2", "s50002", "t50001"]], ["37.61.209.146", "e-1.claudioboxx.com", ["v1.2", "s50002", "t50001"]], ["37.205.8.78", "electrum.taborsky.cz", ["v1.2", "s50002"]], ["88.198.91.74", "currentlane.lovebitco.in", ["v1.2", "s50002", "t50001"]], ["178.32.88.133", "helicarrier.bauerj.eu", ["v1.2", "s50002", "t50001"]], ["84.200.105.74", "daedalus.bauerj.eu", ["v1.2", "s50002", "t50001"]], ["192.30.120.110", "electrum.infinitum-nihil.com", ["v1.0", "s50002"]], ["185.36.252.200", "electrum.eff.ro", ["v1.2", "s50002", "t50001"]], ["173.249.8.197", "bitcoin.grey.pw", ["v1.2", "s50002", "t50001"]], ["176.31.252.219", "rbx.curalle.ovh", ["v1.2", "s50002"]], ["75.159.6.167", "enode.duckdns.org", ["v1.2", "s50002", "t50001"]], ["103.82.56.24", "btc.ex.laodc.com", ["v1.0", "s443"]], ["185.64.116.15", "185.64.116.15", ["v1.2", "s50002", "t50001"]], ["79.11.31.76", "electrumx.soon.it", ["v1.2", "s50002", "t50001"]], ["195.154.173.26", "electrum.chainhost.io", ["v1.2", "s50002"]], ["31.208.128.61", "elx01.knas.systems", ["v1.2", "s50002", "t50001"]], ["141.0.148.11", "aspinall.io", ["v1.2", "s50002"]], ["121.44.94.246", "btc.outoftime.co", ["v1.2", "s50002", "t50001"]], ["69.10.143.103", "node.erratic.space", ["v1.2", "s50002", "t50001"]], ["51.15.138.64", "tardis.bauerj.eu", ["v1.2", "s50002", "t50001"]], ["220.233.178.199", "electrum-server.ninja", ["v1.2", "s50002", "t50001"]], ["94.130.136.185", "alviss.coinjoined.com", ["v1.1", "s50002", "t50001"]], ["172.103.153.90", "electrum.mindspot.org", ["v1.2", "s50002", "t50001"]], ["217.233.81.39", "ndnd.selfhost.eu", ["v1.2", "s50002", "t50001"]], ["173.212.247.250", "electrum.festivaldelhumor.org", ["v1.2", "s50002", "t50001"]], ["88.198.241.196", "electrum.be", ["v1.2", "s50002", "t50001"]], ["81.171.27.138", "spv.48.org", ["v1.2", "s50002", "t50003"]], ["35.161.251.242", "btc1.bitwage.com", ["v1.0", "p100", "t50001"]], ["139.162.14.142", "139.162.14.142", ["v1.1", "s50002", "t50001"]], ["95.211.185.14", "elec.luggs.co", ["v1.2", "s443"]], ["199.59.105.231", "quaternion.tech", ["v1.2", "s50002", "t50001"]], ["54.36.234.239", "ip239.ip-54-36-234.eu", ["v1.1", "s50002", "t50001"]], ["134.119.179.55", "134.119.179.55", ["v1.0", "s50002"]], ["89.205.81.5", "electrumx.hopto.org", ["v1.2", "s50002", "t50001"]], ["83.162.196.192", "electrum0.snel.it", ["v1.2", "s50002", "t50001"]], ["95.122.13.249", "walle.dedyn.io", ["v1.1", "s50002"]], ["163.172.61.154", "such.ninja", ["v1.1", "s50002", "t50001"]], ["169.1.168.124", "electrumx.ddns.net", ["v1.2", "s50002", "t50001"]], ["109.61.102.5", "electrumx.nmdps.net", ["v1.2", "s50002", "t50001"]], ["104.250.141.242", "104.250.141.242", ["v1.1", "s50002"]], ["71.239.123.185", "mooo.not.fyi", ["v1.1", "s50012", "t50011"]], ["118.86.185.36", "yuio.top", ["v1.2", "s50003", "t50001"]], ["162.212.155.122", "electrum2.everynothing.net", ["v1.2", "s50002", "t50001"]], ["78.47.61.83", "kirsche.emzy.de", ["v1.2", "s50002", "t50001"]], ["13.80.67.162", "13.80.67.162", ["v1.2", "s50002", "t50001"]], ["46.246.124.19", "46.246.124.19", ["v1.2", "s50002"]], ["155.4.117.22", "shogoth.no-ip.info", ["v1.2", "s50002", "t50001"]], ["165.227.22.180", "165.227.22.180", ["v1.1", "s50002", "t50001"]], ["88.99.162.199", "electrum.qtornado.com", ["v1.2", "s50002", "t50001"]], ["51.15.77.78", "VPS.hsmiths.com", ["v1.2", "s50002", "t50001"]], ["104.40.216.160", "electrumx.westeurope.cloudapp.azure.com", ["v1.2", "s50002", "t50001"]], ["170.130.28.174", "E-X.not.fyi", ["v1.2", "s50002", "t50001"]], ["50.198.167.205", "btc.smsys.me", ["v1.2", "s995"]], ["67.205.187.44", "electrum.coinucopia.io", ["v1.2", "s50002", "t50001"]], ["88.202.202.221", "electrum.villocq.com", ["v1.2", "s50002", "t50001"]], ["194.71.109.91", "e.keff.org", ["v1.2", "s50002", "t50001"]], ["37.59.46.112", "electrum.vom-stausee.de", ["v1.2", "s50002", "t50001"]], ["88.81.141.191", "electrum2.villocq.com", ["v1.2", "s50002", "t50001"]], ["46.229.238.187", "bitcoins.sk", ["v1.2", "s50002", "t50001"]], ["92.246.24.225", "electrum.dk", ["v1.1", "s50002", "t50001"]], ["54.37.91.119", "ip119.ip-54-37-91.eu", ["v1.1", "s50002", "t50001"]], ["37.61.209.148", "e-3.claudioboxx.com", ["v1.2", "s50002", "t50001"]], ["54.37.91.120", "ip120.ip-54-37-91.eu", ["v1.1", "s50002", "t50001"]], ["207.154.223.80", "207.154.223.80", ["v1.2", "s50002", "t50001"]]]}

如您所见,单个项目包含以下结构

["188.68.38.247", "noserver4u.de", ["v1.2", "s50002", "t50001"]]

一个包含两个字符串的数组和一个包含 3 个字符串的嵌套数组。

如何创建合适的类型来解析响应。

谢谢。

如果您想保留类型信息(而不是使用 interface{}),如果您有字符串和数组的混合数组,则需要使用自定义解组器定义一个类型,该解组器会将数组转换为新类型。我会先解组到 json.RawMessage 的一部分,这样你就可以推迟进一步的解组,所以它变成了一个两步过程。

将这一切放在一个例子中:

package main

import (
    "fmt"
    "encoding/json"
)

const input = `{"result": [["188.68.38.247", "noserver4u.de", ["v1.2", "s50002", "t50001"]], ["37.187.141.73", "electrum.nute.net", ["v1.2", "s50002"]], ["76.174.26.91", "electrum.hsmiths.com", ["v1.2", "s50002", "t50001"]], ["69.27.173.238", "electrum.leblancnet.us", ["v1.2", "s50002", "t50001"]], ["79.219.53.95", "electrum3.hachre.de", ["v1.2", "s50002", "t50001"]], ["178.254.34.161", "v25437.1blu.de", ["v1.2", "s50002"]], ["50.35.67.146", "orannis.com", ["v1.2", "s50002", "t50001"]], ["18.206.2.52", "18.206.2.52", ["v1.2", "s50002", "t50001"]], ["96.27.8.242", "technetium.network", ["v1.2", "s50002"]], ["5.9.124.124", "hetzner01.fischl-online.de", ["v1.2", "s50002"]], ["213.168.187.27", "electrum.petrkr.net", ["v1.2", "s50002", "t50001"]], ["82.217.214.215", "Bitcoin-node.nl", ["v1.1", "s50002", "t50001"]], ["173.91.90.62", "electrumx.bot.nu", ["v1.2", "s50002", "t50001"]], ["37.61.209.146", "e-1.claudioboxx.com", ["v1.2", "s50002", "t50001"]], ["37.205.8.78", "electrum.taborsky.cz", ["v1.2", "s50002"]], ["88.198.91.74", "currentlane.lovebitco.in", ["v1.2", "s50002", "t50001"]], ["178.32.88.133", "helicarrier.bauerj.eu", ["v1.2", "s50002", "t50001"]], ["84.200.105.74", "daedalus.bauerj.eu", ["v1.2", "s50002", "t50001"]], ["192.30.120.110", "electrum.infinitum-nihil.com", ["v1.0", "s50002"]], ["185.36.252.200", "electrum.eff.ro", ["v1.2", "s50002", "t50001"]], ["173.249.8.197", "bitcoin.grey.pw", ["v1.2", "s50002", "t50001"]], ["176.31.252.219", "rbx.curalle.ovh", ["v1.2", "s50002"]], ["75.159.6.167", "enode.duckdns.org", ["v1.2", "s50002", "t50001"]], ["103.82.56.24", "btc.ex.laodc.com", ["v1.0", "s443"]], ["185.64.116.15", "185.64.116.15", ["v1.2", "s50002", "t50001"]], ["79.11.31.76", "electrumx.soon.it", ["v1.2", "s50002", "t50001"]], ["195.154.173.26", "electrum.chainhost.io", ["v1.2", "s50002"]], ["31.208.128.61", "elx01.knas.systems", ["v1.2", "s50002", "t50001"]], ["141.0.148.11", "aspinall.io", ["v1.2", "s50002"]], ["121.44.94.246", "btc.outoftime.co", ["v1.2", "s50002", "t50001"]], ["69.10.143.103", "node.erratic.space", ["v1.2", "s50002", "t50001"]], ["51.15.138.64", "tardis.bauerj.eu", ["v1.2", "s50002", "t50001"]], ["220.233.178.199", "electrum-server.ninja", ["v1.2", "s50002", "t50001"]], ["94.130.136.185", "alviss.coinjoined.com", ["v1.1", "s50002", "t50001"]], ["172.103.153.90", "electrum.mindspot.org", ["v1.2", "s50002", "t50001"]], ["217.233.81.39", "ndnd.selfhost.eu", ["v1.2", "s50002", "t50001"]], ["173.212.247.250", "electrum.festivaldelhumor.org", ["v1.2", "s50002", "t50001"]], ["88.198.241.196", "electrum.be", ["v1.2", "s50002", "t50001"]], ["81.171.27.138", "spv.48.org", ["v1.2", "s50002", "t50003"]], ["35.161.251.242", "btc1.bitwage.com", ["v1.0", "p100", "t50001"]], ["139.162.14.142", "139.162.14.142", ["v1.1", "s50002", "t50001"]], ["95.211.185.14", "elec.luggs.co", ["v1.2", "s443"]], ["199.59.105.231", "quaternion.tech", ["v1.2", "s50002", "t50001"]], ["54.36.234.239", "ip239.ip-54-36-234.eu", ["v1.1", "s50002", "t50001"]], ["134.119.179.55", "134.119.179.55", ["v1.0", "s50002"]], ["89.205.81.5", "electrumx.hopto.org", ["v1.2", "s50002", "t50001"]], ["83.162.196.192", "electrum0.snel.it", ["v1.2", "s50002", "t50001"]], ["95.122.13.249", "walle.dedyn.io", ["v1.1", "s50002"]], ["163.172.61.154", "such.ninja", ["v1.1", "s50002", "t50001"]], ["169.1.168.124", "electrumx.ddns.net", ["v1.2", "s50002", "t50001"]], ["109.61.102.5", "electrumx.nmdps.net", ["v1.2", "s50002", "t50001"]], ["104.250.141.242", "104.250.141.242", ["v1.1", "s50002"]], ["71.239.123.185", "mooo.not.fyi", ["v1.1", "s50012", "t50011"]], ["118.86.185.36", "yuio.top", ["v1.2", "s50003", "t50001"]], ["162.212.155.122", "electrum2.everynothing.net", ["v1.2", "s50002", "t50001"]], ["78.47.61.83", "kirsche.emzy.de", ["v1.2", "s50002", "t50001"]], ["13.80.67.162", "13.80.67.162", ["v1.2", "s50002", "t50001"]], ["46.246.124.19", "46.246.124.19", ["v1.2", "s50002"]], ["155.4.117.22", "shogoth.no-ip.info", ["v1.2", "s50002", "t50001"]], ["165.227.22.180", "165.227.22.180", ["v1.1", "s50002", "t50001"]], ["88.99.162.199", "electrum.qtornado.com", ["v1.2", "s50002", "t50001"]], ["51.15.77.78", "VPS.hsmiths.com", ["v1.2", "s50002", "t50001"]], ["104.40.216.160", "electrumx.westeurope.cloudapp.azure.com", ["v1.2", "s50002", "t50001"]], ["170.130.28.174", "E-X.not.fyi", ["v1.2", "s50002", "t50001"]], ["50.198.167.205", "btc.smsys.me", ["v1.2", "s995"]], ["67.205.187.44", "electrum.coinucopia.io", ["v1.2", "s50002", "t50001"]], ["88.202.202.221", "electrum.villocq.com", ["v1.2", "s50002", "t50001"]], ["194.71.109.91", "e.keff.org", ["v1.2", "s50002", "t50001"]], ["37.59.46.112", "electrum.vom-stausee.de", ["v1.2", "s50002", "t50001"]], ["88.81.141.191", "electrum2.villocq.com", ["v1.2", "s50002", "t50001"]], ["46.229.238.187", "bitcoins.sk", ["v1.2", "s50002", "t50001"]], ["92.246.24.225", "electrum.dk", ["v1.1", "s50002", "t50001"]], ["54.37.91.119", "ip119.ip-54-37-91.eu", ["v1.1", "s50002", "t50001"]], ["37.61.209.148", "e-3.claudioboxx.com", ["v1.2", "s50002", "t50001"]], ["54.37.91.120", "ip120.ip-54-37-91.eu", ["v1.1", "s50002", "t50001"]], ["207.154.223.80", "207.154.223.80", ["v1.2", "s50002", "t50001"]]]}`

type data struct {
    Items []*item `json:"result"`
}

type item struct {
    Value1 string
    Value2 string
    Value3 []string
}

func (i *item) UnmarshalJSON(b []byte) error {
    // Use a slice of RawMessage to delay parsing of nested values that are different types
    var raw []json.RawMessage
    if err := json.Unmarshal(b, &raw); err != nil {
        return err
    }

    // Now grab the nested values using the right types for each
    var value1 string
    var value2 string
    var value3 []string
    if err := json.Unmarshal(raw[0], &value1); err != nil {
        return err
    }
    if err := json.Unmarshal(raw[1], &value2); err != nil {
        return err
    }
    if err := json.Unmarshal(raw[2], &value3); err != nil {
        return err
    }

    *i = item{value1, value2, value3}

    return nil
}

func main() {
    var result data
    json.Unmarshal([]byte(input), &result)
    fmt.Printf("Item1:\n\tvalue1: %s\n\tvalue2: %s\n\tvalue3: %s\n", result.Items[0].Value1, result.Items[0].Value2, result.Items[0].Value3)
}

您可以在此处 运行:

https://play.golang.org/p/WG-kgXNMapQ

进一步阅读 JSON 的文档:

https://golang.org/pkg/encoding/json/#example__customMarshalJSON https://golang.org/pkg/encoding/json/#example_RawMessage_unmarshal