使用Golang的encoding/json包读取嵌套的JSON数据。

huangapple go评论74阅读模式
英文:

Reading nested json data with golang's encoding/json

问题

我无法获得正确的定义来捕获保存在变量中的嵌套json数据。我的代码片段如下:

package main

import "fmt"
import "encoding/json"

type Data struct {
    P        string `json:"ports"`
    Ports    struct {
        Portnums []int
    }
    Protocols []string `json:"protocols"`
}

func main() {
    y := `{
        "ports": {
            "udp": [
                1,
                30
            ],
            "tcp": [
                100,
                1023
            ]
        },
        "protocols": [
            "tcp",
            "udp"
        ]
    }`
    var data Data
    e := json.Unmarshal([]byte(y), &data)
    if e == nil {
        fmt.Println(data)
    } else {
        fmt.Println("Failed:", e)
    }
}

$ go run foo.go
Failed: json: cannot unmarshal object into Go value of type string

这是你的代码片段,我已经将其翻译成中文。

英文:

I can't get the correct definition for my structs to capture the nested json data saved in a variable. My code snippet is as below:

package main

import "fmt"
import "encoding/json"

type Data struct {
	P string `json:"ports"`
	Ports struct {
         Portnums []int
    }
	Protocols []string `json:"protocols"`
}

func main() {
		y := `{
	    		"ports": {
			"udp": [
			    1, 
			    30
			], 
			"tcp": [
			    100, 
			    1023
			]
		    }, 
		    "protocols": [
			"tcp", 
			"udp"
		    ]
	}`
	var data Data
	e := json.Unmarshal([]byte(y), &data)
	if e == nil {
		fmt.Println(data)
	} else {
		fmt.Println("Failed:", e)
	}

}

$ go run foo.go 
Failed: json: cannot unmarshal object into Go value of type string

答案1

得分: 2

这对我来说没问题(请参阅上面对你问题的评论)GoPlay

type Data struct {
    Ports struct {
        Tcp []float64 `json:"tcp"`
        Udp []float64 `json:"udp"`
    } `json:"ports"`
    Protocols []string `json:"protocols"`
}

func main() {
    y := `{
        "ports": {
            "udp": [
                1,
                30
            ],
            "tcp": [
                100,
                1023
            ]
        },
        "protocols": [
            "tcp",
            "udp"
        ]
    }`
    d := Data{}
    err := json.Unmarshal([]byte(y), &d)
    if err != nil {
        fmt.Println("Error:", err.Error())
    } else {
        fmt.Printf("%#+v", d)
    }
}

输出

main.Data{
    Ports: struct {
        Tcp []float64 "json:\"tcp\""
        Udp []float64 "json:\"udp\""
    }{
        Tcp: []float64{100, 1023},
        Udp: []float64{1, 30}
    },
    Protocols: []string{"tcp", "udp"}
}
英文:

This works for me (see comment to your question above) <kbd>GoPlay</kbd>

type Data struct {
	Ports struct {
		Tcp []float64 `json:&quot;tcp&quot;`
		Udp []float64 `json:&quot;udp&quot;`
	} `json:&quot;ports&quot;`
	Protocols []string `json:&quot;protocols&quot;`
}

func main() {
	y := `{
                &quot;ports&quot;: {
            &quot;udp&quot;: [
                1, 
                30
            ], 
            &quot;tcp&quot;: [
                100, 
                1023
            ]
            }, 
            &quot;protocols&quot;: [
            &quot;tcp&quot;, 
            &quot;udp&quot;
            ]
	}`
	d := Data{}
	err := json.Unmarshal([]byte(y), &amp;d)
	if err != nil {
		fmt.Println(&quot;Error:&quot;, err.Error())
	} else {
		fmt.Printf(&quot;%#+v&quot;, d)
	}

}

OUTPUT

main.Data{
    Ports:struct { 
        Tcp []float64 &quot;json:\&quot;tcp\&quot;&quot;;
        Udp []float64 &quot;json:\&quot;udp\&quot;&quot; 
    }{
        Tcp:[]float64{100, 1023},
        Udp:[]float64{1, 30}
    },
    Protocols:[]string{&quot;tcp&quot;, &quot;udp&quot;}
}

huangapple
  • 本文由 发表于 2015年3月14日 04:56:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/29041789.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定