How to decode json in Go

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

How to decode json in Go

问题

你可以使用json.Unmarshal函数将JSON解码为一个map[string]interface{}类型的变量。这样你就可以得到一个映射而不是切片。以下是修改后的代码示例:

bt := []byte(metadatas[0])
var dat []map[string]interface{}
if err := json.Unmarshal(bt, &dat); err != nil {
    panic(err)
}
fmt.Println(dat)

这样,你将得到一个[]map[string]interface{}类型的变量dat,其中每个元素都是一个映射,表示JSON中的对象。

英文:

I have a json look like this

[{"name":"Name1","age":20},{"name":"Name2","age":29}]

And I want to decode it to map look like this

map[map["name":"Name1" ....] ,map["name":"Name2",....]]

In my case I have a logic look like this

bt:= []byte(metadatas[0])
var dat interface{}
if err := json.Unmarshal(bt, dat); err != nil {
	panic(err)
}
fmt.Println(dat)

and as a response I am getting

[map["name":"Name1" ....] ,map["name":"Name2",....]]

How can I get a map instead of

答案1

得分: 2

一个用于解组的目标数据结构可以定义为 val := []map[string]interface{}{}

package main

import (
	"encoding/json"
	"fmt"
)

func main() {
	input := []byte(`[{"name":"Name1","age":20},{"name":"Name2","age":29}]`)
	val := []map[string]interface{}{}
	if err := json.Unmarshal(input, &val); err != nil {
		panic(err)
	}
	fmt.Printf("%v\n", val)
}

Go Playground

这将给我们一个映射的切片。如果你想将这些映射放入另一个映射中,那么应该为该映射的元素提供一个键(例如它们在切片中的索引)。

英文:

A target data structure for unmarshaling can be defined as val := []map[string]interface{}{}:

package main

import (
	"encoding/json"
	"fmt"
)

func main() {
	input := []byte(`[{"name":"Name1","age":20},{"name":"Name2","age":29}]`)
	val := []map[string]interface{}{}
	if err := json.Unmarshal(input, &val); err != nil {
		panic(err)
	}
	fmt.Printf("%v\n", val)
}

Go Playgroung

This gives us a slice of maps. If you want to put those maps inside another map, there should be a key for elements of that map (like their indexes in the slice).

答案2

得分: 1

根据定义,map 是一种使用预定义的键来保留对特定值的引用的数据结构。

在你的情况下,你有一个名为 collection 的数据结构(一组 map)。如果你想将集合项保留为另一个 map 的值,最简单的方法是使用数组(collection)的索引将集合转换为 map。

然而,我不确定你是否能够在 json.Unmarshal 过程中直接完成这个操作,可能需要进行一些额外的转换。

英文:

By definition map is a data structure that keeps a reference to specific values using some predefined keys.

In your case, you're having a data structure called collection (array of maps). If you want to keep your collection items as another map values the easiest way to achieve this is to transform your collection into map using indexes of the array (collection) as keys.

However I'm not sure you would be able to do it straight-head during your json.Unmarshal without applying some additional transformations

答案3

得分: 0

我已经解决了这个问题,这是我的逻辑:

type Meta struct {
    Name string `json:"name"`
    Age int `json:"age"`
}

type Metadata struct {
    Collection []Meta
}

bt := []byte(metadatas[0])
dat := make([]Meta, 0)
if err := json.Unmarshal(bt, &dat); err != nil {
    panic(err)
}

作为响应,我得到了一个包含对象的数组。

英文:

I have already fix this problem, Here is my logic

type Meta struct {
	Name string `json:"name"`
	Age int `json:"age"`
}

type Metadata struct {
	Collection []Meta
}


bt:= []byte(metadatas[0])
dat := make([]Meta, 0)
if err := json.Unmarshal(bt, &dat); err != nil {
	panic(err)
}

And as a response I am getting array with objects

huangapple
  • 本文由 发表于 2017年7月26日 15:32:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/45320313.html
匿名

发表评论

匿名网友

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

确定