英文:
How to change the value of a key from int to string in json
问题
在golang中,我想要将JSON中一个键的值从整数改为字符串。
输入:
{
"id": 12345,
"wrapper": 898984,
"sections": {
"main": {
"type": 76899
}
},
"order": [
82322
]
}
期望的输出:
{
"id": "12345",
"wrapper": "898984",
"sections": {
"main": {
"type": "76899"
}
},
"order": [
"82322"
]
}
英文:
In this json I want to change the value of an key from int to string in golang.
Input:
{
"id": 12345,
"wrapper": 898984,
"sections": {
"main": {
"type": 76899
}
},
"order": [
82322
]
}
Desired output:
{
"id": "12345",
"wrapper": "898984",
"sections": {
"main": {
"type": "76899"
}
},
"order": [
"82322"
]
}
答案1
得分: 2
最直接的方法是为两个JSON创建结构体。然后创建一个将一个转换为另一个的函数:
package main
import (
"encoding/json"
"fmt"
"strconv"
)
var input = `{ "id": 12345, "wrapper": 898984, "sections": { "main": { "type": 76899 } }, "order": [ 82322 ] }`
type DataWithInts struct {
ID int `json:"id"`
Wrapper int `json:"wrapper"`
Sections struct {
Main struct {
Type int `json:"type"`
} `json:"main"`
} `json:"sections"`
Order []int `json:"order"`
}
type MainStringsData struct {
Type string `json:"type"`
}
type SectionsStringsData struct {
Main MainStringsData `json:"main"`
}
type DataWithStrings struct {
ID string `json:"id"`
Wrapper string `json:"wrapper"`
Sections SectionsStringsData `json:"sections"`
Order []string `json:"order"`
}
func GetDataWithStrings(data *DataWithInts) *DataWithStrings {
var order []string
for _, v := range data.Order {
order = append(order, strconv.Itoa(v))
}
return &DataWithStrings{
ID: strconv.Itoa(data.ID),
Wrapper: strconv.Itoa(data.Wrapper),
Sections: SectionsStringsData{
Main: MainStringsData{
Type: strconv.Itoa(data.Sections.Main.Type),
},
},
Order: order,
}
}
func main() {
var dataInts DataWithInts
err := json.Unmarshal([]byte(input), &dataInts)
if err != nil {
panic(err)
}
dataStrings := GetDataWithStrings(&dataInts)
jsonData, err := json.Marshal(dataStrings)
if err != nil {
panic(err)
}
fmt.Println(string(jsonData))
}
更通用的方法可以使用直接的JSON解析或反射包。
附注:
可以使用以下网站创建结构体:https://mholt.github.io/json-to-go/
英文:
The most straightforward way is to create structs for both jsons. Then create function that converts one to another:
package main
import (
"encoding/json"
"fmt"
"strconv"
)
var input = `{ "id": 12345, "wrapper": 898984, "sections": { "main": { "type": 76899 } }, "order": [ 82322 ] }`
type DataWithInts struct {
ID int `json:"id"`
Wrapper int `json:"wrapper"`
Sections struct {
Main struct {
Type int `json:"type"`
} `json:"main"`
} `json:"sections"`
Order []int `json:"order"`
}
type MainStringsData struct {
Type string `json:"type"`
}
type SectionsStringsData struct {
Main MainStringsData `json:"main"`
}
type DataWithStrings struct {
ID string `json:"id"`
Wrapper string `json:"wrapper"`
Sections SectionsStringsData `json:"sections"`
Order []string `json:"order"`
}
func GetDataWithStrings(data *DataWithInts) *DataWithStrings {
var order []string
for _, v := range data.Order {
order = append(order, strconv.Itoa(v))
}
return &DataWithStrings{
ID: strconv.Itoa(data.ID),
Wrapper: strconv.Itoa(data.Wrapper),
Sections: SectionsStringsData{
Main: MainStringsData{
Type: strconv.Itoa(data.Sections.Main.Type),
},
},
Order: order,
}
}
func main() {
var dataInts DataWithInts
err := json.Unmarshal([]byte(input), &dataInts)
if err != nil {
panic(err)
}
dataStrings := GetDataWithStrings(&dataInts)
jsonData, err := json.Marshal(dataStrings)
if err != nil {
panic(err)
}
fmt.Println(string(jsonData))
}
more universal approach can use direct json parsing or reflect package.
P.S.
To create structs, following site can be used: https://mholt.github.io/json-to-go/
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论