英文:
Constant time search deeply nested JSON data
问题
我有一个嵌套的JSON结构,像下面这样。
{"object":
{
"buffer_size": 10,
"Databases":
[
{
"host": "localhost",
"user": "root",
"pass": "",
"type": "mysql",
"name": "go",
"Tables":
[
{
"name": "testing",
"statment": "teststring",
"regex": "teststring ([0-9]+) ([A-z]+)",
"Types":
[
{
"id": "1",
"value": "string"
},
{
"id": "2",
"value": "string"
},
{
"id": "3",
"value": "string"
}
]
}
]
}
]
}
}
有没有办法使用常数时间查找Types
的id
并获取过滤后的结果,只输出那些扁平化的JSON中的结果,如下所示:
{
"Types": [
{"id": 1, "value": "string"},
{"id": 2, "value": "string"},
{"id": 3, "value": "string"}
]
}
我知道可以使用for循环来实现,但那样会花费很长时间。
英文:
I have a deeply nested structure of nested JSON like below.
{"object":
{
"buffer_size": 10,
"Databases":
[
{
"host": "localhost",
"user": "root",
"pass": "",
"type": "mysql",
"name": "go",
"Tables":
[
{
"name": "testing",
"statment": "teststring",
"regex": "teststring ([0-9]+) ([A-z]+)",
"Types":
[
{
"id": "1",
"value": "string"
},
{
"id": "2",
"value": "string"
},
{
"id": "3",
"value": "string"
}
]
}
]
}
]
}
}
Is there a way I can use constant time lookup to search for id
of Types
and get the filtered results just for those ones with flattened JSON so that it outputs:
{
"Types": [
{"id": 1, "value": "string"}
{"id": 2, "value": "string"}
{"id": 3, "value": "string"}
]
}
I know I can achieve by using for loop but that would take long time.
答案1
得分: 1
这是我会做的方式:
type Id int
func (i *Id) UnmarshalJSON(b []byte) error {
s := strings.Trim(string(b), `"`)
id, err := strconv.Atoi(s)
*i = Id(id)
return err
}
type Type struct {
Id Id `json:"id"`
Value string `json:"value"`
}
func getTypes(b []byte) ([]Type, error) {
var types = []Type{}
var obj struct {
Object struct {
Databases []struct {
Tables []struct {
Types []Type `json:"Types"`
} `json:"Tables"`
} `json:"Databases"`
} `json:"object"`
}
if err := json.Unmarshal(b, &obj); err != nil {
return nil, err
}
for _, d := range obj.Object.Databases {
for _, t := range d.Tables {
types = append(types, t.Types...)
}
}
return types, nil
}
func showTypes(types []Type) error {
return json.NewEncoder(os.Stdout).Encode(struct {
Types []Type `json:"Types"`
}{types})
}
func main() {
types, err := getTypes([]byte(data))
if err != nil {
log.Fatal(err)
}
if err := showTypes(types); err != nil {
log.Fatal(err)
}
}
至于常数时间,那是不可能的。
英文:
Here's how I'd do it:
type Id int
func (i *Id) UnmarshalJSON(b []byte) error {
s := strings.Trim(string(b), `"`)
id, err := strconv.Atoi(s)
*i = Id(id)
return err
}
type Type struct {
Id Id `json:"id"`
Value string `json:"value"`
}
func getTypes(b []byte) ([]Type, error) {
var types = []Type{}
var obj struct {
Object struct {
Databases []struct {
Tables []struct {
Types []Type `json:"Types"`
} `json:"Tables"`
} `json:"Databases"`
} `json:"object"`
}
if err := json.Unmarshal(b, &obj); err != nil {
return nil, err
}
for _, d := range obj.Object.Databases {
for _, t := range d.Tables {
types = append(types, t.Types...)
}
}
return types, nil
}
func showTypes(types []Type) error {
return json.NewEncoder(os.Stdout).Encode(struct {
Types []Type `json:"Types"`
}{types})
}
func main() {
types, err := getTypes([]byte(data))
if err != nil {
log.Fatal(err)
}
if err := showTypes(types); err != nil {
log.Fatal(err)
}
}
As far as constant time goes, that's simply not possible.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论