从Golang中的多行json文件中创建值列表



我需要从JSON输入创建一个值列表。例如,如果输入是:(这是一个文件。下面的内容是针对这个问题提到的(

x := `[
{
"customer_id": "g62"
},
{           
"customer_id": "b23"
},
{           
"customer_id": "a34"
},
{       
"customer_id": "c42"
}

]`

输出需要采用json格式:{"Customer_id":["g62"、"b23"、"a34"、"c42"]}

我想出了一个代码,它给出了以下内容:[{"租户id":"7645","客户id":["g62","b23","a34","c42"]}]

我有一个额外的Tenant_id字段,我认为这段代码效率不高,因为我需要创建一个映射,然后创建一个列表来实现这一点。我在网上找了很多例子,但找不到任何能帮助我做得更好的具体例子。我在这里的目标是让它变得高效,并摆脱一个额外的领域;tenant_id";。如有任何意见或帮助,我们将不胜感激。

注意:我之所以使用tenant_id,是因为我需要一个映射的密钥。我正在寻找一个替代的解决方案或想法!

下面的代码正在工作:

package main
import (
"encoding/json"
"fmt"
)

type InpData struct {
Tenant_id   string
Customer_id []string
}

type InpList []InpData

func main() {
x := `[
{
"customer_id": "g62"
},
{           
"customer_id": "b23"
},
{           
"customer_id": "a34"
},
{       
"customer_id": "c42"
}

]`
var v InpList
if err := json.Unmarshal([]byte(x), &v); err != nil {
fmt.Println(err)
}
fmt.Printf("%+v", v)
fmt.Println("nJson out of list:")
fmt.Println()
j, err := json.Marshal(v)
if err != nil {
fmt.Printf("Error: %s", err.Error())
} else {
fmt.Println(string(j))
}
}
func (v *InpList) UnmarshalJSON(b []byte) error {
// Create a local struct that mirrors the data being unmarshalled
type mciEntry struct {
Tenant_id   string `json:"tenant_id"`
Customer_id string `json:"customer_id"`
}
var tenant_id string
tenant_id = "7645"
type InpSet map[string][]string
var entries []mciEntry
// unmarshal the data into the slice
if err := json.Unmarshal(b, &entries); err != nil {
return err
}
tmp := make(InpSet)
// loop over the slice and create the map of entries
for _, ent := range entries {
tmp[tenant_id] = append(tmp[tenant_id], ent.Customer_id)
}
fmt.Println()
tmpList := make(InpList, 0, len(tmp))
for key, value := range tmp {
tmpList = append(tmpList, InpData{Tenant_id: key, Customer_id: value})
}
*v = tmpList
return nil

}

它似乎不应该比这样的东西更复杂:

https://goplay.tools/snippet/IhRc4tV9UH0

package main
import (
"encoding/json"
"fmt"
)
type Input struct {
CustomerId string `json:"customer_id"`
}
type InputList []Input
type Output struct {
CustomerIds []string `json:"Customer_id"`
}
func main() {
jsonSource := `[
{ "customer_id": "g62" },
{ "customer_id": "b23" },
{ "customer_id": "a34" },
{ "customer_id": "c42" }
]`
input := deserialize(jsonSource)
fmt.Println()
fmt.Println("Input:")
fmt.Printf("%+vn", input)
output := transform(input)
fmt.Println()
fmt.Println("Output:")
fmt.Printf("%+vn", output)
jsonFinal := serialize(output)
fmt.Println()
fmt.Println("Final JSON:")
fmt.Println(jsonFinal)
}
func deserialize(s string) InputList {
var input InputList
if err := json.Unmarshal([]byte(s), &input); err != nil {
panic(err)
}
return input
}
func transform(input InputList) Output {
output := Output{
CustomerIds: make([]string, 0, len(input)),
}
for _, item := range input {
output.CustomerIds = append(output.CustomerIds, item.CustomerId)
}
return output
}
func serialize(output Output) string {
buf, err := json.Marshal(output)
if err != nil {
panic(err)
}
s := string(buf)
return s
}

最新更新