指针数组指向 JSON

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

Array of pointers to JSON

问题

在golang中,我有一个指向结构体的二维切片,如下所示:

type point struct {
    x int
    y int
}

type cell struct {
    point   point
    visited bool
    walls   walls
}

type walls struct {
    n bool
    e bool
    s bool
    w bool
}

type maze struct {
    cells         [][]*cell
    solutionStack Stack
}

我想将cells切片序列化为JSON。但是由于所有元素都是指针,调用encode将得到空的JSON。有什么好的方法可以序列化这个切片呢?

一个解决方案是创建这个二维切片的本地副本,并用实际的结构体替换所有指针。这样可以工作,但不是最优解。

英文:

In golang I have two dimensional slice of pointers to struct, as shown in code below.

type point struct {
	x int
	y int
}

type cell struct {
	point   point
	visited bool
	walls   walls
}

type walls struct {
	n bool
	e bool
	s bool
	w bool
}

type maze struct {
	cells         [][]*cell
	solutionStack Stack
}

I would like to serialize cells slice into JSON. But as all the elements are pointers calling encode will give empty JSON. What would be the best way to serialize this slice.

One solution that comes to my mid is to create a local copy of this 2D slice ad replace all pointers with actual struct. It'll work but it is no

答案1

得分: 3

我不确定我是否回答了你的问题,因为内置的JSON包会自动反射指针。它应该“只是工作”。我注意到你在结构体中没有导出属性,也许这是你的问题所在?在使用反射时,你不能检查未导出的值。

package main

import (
	"encoding/json"
	"fmt"
)

type point struct {
	X int
	Y int
}

type cell struct {
	Point   point
	Visited bool
	Walls   walls
}

type walls struct {
	N bool
	E bool
	S bool
	W bool
}

type maze struct {
	Cells [][]*cell
}

func main() {
	m := maze{}

	var row1 []*cell
	var row2 []*cell

	row1 = append(row1, &cell{
		Point: point{1, 2},
		Walls: walls{N: true},
	})
	row2 = append(row2, &cell{
		Point: point{3, 4},
		Walls: walls{E: true},
	})
	m.Cells = append(m.Cells, row1, row2)

	mazeJson, _ := json.MarshalIndent(m, "", "  ")
	fmt.Println(string(mazeJson))
}

我已经将代码翻译成中文,你可以查看上面的代码。

英文:

I'm not sure if I'm answering your question because the built in JSON package will do the reflection of the pointers automatically. It should "just work". I did notice that you are not exporting the properties in your struct, maybe that is the issue you have? When using reflection, you cannot inspect unexported values.

http://play.golang.org/p/zTuMLBgGWk

package main

import (
	"encoding/json"
	"fmt"
)

type point struct {
	X int
	Y int
}

type cell struct {
	Point   point
	Visited bool
	Walls   walls
}

type walls struct {
	N bool
	E bool
	S bool
	W bool
}

type maze struct {
	Cells [][]*cell
}

func main() {
	m := maze{}

	var row1 []*cell
	var row2 []*cell

	row1 = append(row1, &cell{
		Point: point{1, 2},
		Walls: walls{N: true},
	})
	row2 = append(row2, &cell{
		Point: point{3, 4},
		Walls: walls{E: true},
	})
	m.Cells = append(m.Cells, row1, row2)

	mazeJson, _ := json.MarshalIndent(m, "", "  ")
	fmt.Println(string(mazeJson))
}

huangapple
  • 本文由 发表于 2015年1月23日 10:09:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/28101966.html
匿名

发表评论

匿名网友

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

确定