How to change the value of a key from int to string in json

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

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/

huangapple
  • 本文由 发表于 2022年11月3日 13:25:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/74298454.html
匿名

发表评论

匿名网友

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

确定