如何在GOlang中替换键值映射中的json值
我有下面的json&我需要从映射中将员工的名字替换为emp\u id 尝试了此操作,但不确定如何替换给定映射中的值,以及在给定映射中不存在该值时的错误处理 Json数据: 地图数据: [{emp1 14325}{emp3 49184}{emp2 21518}] 预期产出: 代码: 从下面的代码开始,但不确定如何使用给定的映射替换为错误处理如何在GOlang中替换键值映射中的json值,go,Go,我有下面的json&我需要从映射中将员工的名字替换为emp\u id 尝试了此操作,但不确定如何替换给定映射中的值,以及在给定映射中不存在该值时的错误处理 Json数据: 地图数据: [{emp1 14325}{emp3 49184}{emp2 21518}] 预期产出: 代码: 从下面的代码开始,但不确定如何使用给定的映射替换为错误处理 package main import ( "encoding/json" "fmt"
package main
import (
"encoding/json"
"fmt"
//"strconv"
//"log"
)
func main() {
jsonStr := `[
{
"dept": "IT",
"condition": {
"employee": [
"emp1"
]
}
},
{
"dept": "HR",
"condition": {
"employee": [
"emp2",
"emp3"
]
}
}
]`
empMap := `[{emp1 14325} {emp3 49184} {emp2 21518}]`
type GetEmployee []struct {
Dept string `json:"dept"`
Condition struct {
Employee []string `json:"employee"`
} `json:"condition"`
}
var empResponse GetEmployee
unmarshallingError := json.Unmarshal([]byte(string(jsonStr)), &empResponse)
if unmarshallingError != nil {
fmt.Println(unmarshallingError.Error())
}
fmt.Println(empResponse)
fmt.Println(empMap)
for i := range empResponse {
fmt.Println(i)
}
}
与其将ID存储在
{ids:value}
数组中,不如将它们存储在映射中
以上内容将覆盖员工姓名,并将其更改为id。检查地图中是否有特定的id键
for i, e := range empResponse {
fmt.Println(e)
for j,val := range empResponse[i].Condition.Employee {
if _, ok := ids[val]; ok {
empResponse[i].Condition.Employee[j] = ids[val]
}
}
}
完整代码
package main
import (
"encoding/json"
"fmt"
//"strconv"
//"log"
)
func main() {
jsonStr := `[
{
"dept": "IT",
"condition": {
"employee": [
"emp1"
]
}
},
{
"dept": "HR",
"condition": {
"employee": [
"emp2",
"emp3"
]
}
}
]`
empMap := `{"emp1": "14325", "emp3": "49184", "emp2": "21518"}`
type GetEmployee []struct {
Dept string `json:"dept"`
Condition struct {
Employee []string `json:"employee"`
} `json:"condition"`
}
var empResponse GetEmployee
var ids map[string]string
unmarshallingError := json.Unmarshal([]byte(string(jsonStr)), &empResponse)
if unmarshallingError != nil {
fmt.Println(unmarshallingError.Error())
}
json.Unmarshal([]byte(empMap), &ids)
fmt.Println(empResponse)
fmt.Println(ids)
for i, e := range empResponse {
fmt.Println(e)
for j,val := range empResponse[i].Condition.Employee {
if _, ok := ids[val]; ok {
empResponse[i].Condition.Employee[j] = ids[val]
}
}
}
fmt.Println(empResponse)
}
在上面的示例中,id是字符串,因为要替换的名称是字符串。实际上员工
属于[]字符串类型
。如果字符串要替换为int,则需要将员工类型更改为[]接口{}
与其将ID存储在
{id:value}
数组中,不如将它们存储在映射中
以上内容将覆盖员工姓名,并将其更改为id。检查地图中是否有特定的id键
for i, e := range empResponse {
fmt.Println(e)
for j,val := range empResponse[i].Condition.Employee {
if _, ok := ids[val]; ok {
empResponse[i].Condition.Employee[j] = ids[val]
}
}
}
完整代码
package main
import (
"encoding/json"
"fmt"
//"strconv"
//"log"
)
func main() {
jsonStr := `[
{
"dept": "IT",
"condition": {
"employee": [
"emp1"
]
}
},
{
"dept": "HR",
"condition": {
"employee": [
"emp2",
"emp3"
]
}
}
]`
empMap := `{"emp1": "14325", "emp3": "49184", "emp2": "21518"}`
type GetEmployee []struct {
Dept string `json:"dept"`
Condition struct {
Employee []string `json:"employee"`
} `json:"condition"`
}
var empResponse GetEmployee
var ids map[string]string
unmarshallingError := json.Unmarshal([]byte(string(jsonStr)), &empResponse)
if unmarshallingError != nil {
fmt.Println(unmarshallingError.Error())
}
json.Unmarshal([]byte(empMap), &ids)
fmt.Println(empResponse)
fmt.Println(ids)
for i, e := range empResponse {
fmt.Println(e)
for j,val := range empResponse[i].Condition.Employee {
if _, ok := ids[val]; ok {
empResponse[i].Condition.Employee[j] = ids[val]
}
}
}
fmt.Println(empResponse)
}
在上面的示例中,id是字符串,因为要替换的名称是字符串。实际上员工
属于[]字符串类型
。如果字符串要替换为int,则需要将员工类型更改为[]接口{}
@MuffinTop已添加。@MuffinTop已添加。谢谢。如何将替换的值替换为整数,在替换“emp1”-->14325而不是“14325”时是否有任何方法更改结构类型?谢谢。如何将替换的值替换为整数,在替换“emp1”-->14325而不是“14325”时,是否有方法更改结构类型?