在Golang中将字符串解组为类似结构的结构体。

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

Unmarshaling string to struct like structure in Golang

问题

我得到了以下的Golang字符串:

var cars = [
    {
       model: "SLR",
       brand: "Mercedes",
       prices: [520, 730],
    },
    {
       model: "M4",
       brand: "BMW",
       prices: [420, 820],
    }
]

我知道这不是JSON字符串。
有没有办法“解组”这个字符串并获取每辆车的型号?
我该如何获取每辆车的型号?
或者这本来就是一个错误的问题?我很想听听你的意见。

英文:

I was given a string below with Golang:

var cars = [
    {
       model: "SLR",
       brand: "Mercedes",
       prices: [520, 730],
    },
    {
       model: "M4",
       brand: "BMW",
       prices: [420, 820],
    }
]

I know it is not JSON string.
Is there any way at all to "unmarshal" the string and get the models of each car?
How can I get the model of each car?
or is it a wrong question in the first place? I appreciate your opinion.

答案1

得分: 4

为了解析您的输入并获取每辆汽车的型号,您需要使用unmarshal方法和range方法。我已经按照您的情况进行了重现,代码如下:

package main

import (
	"fmt"

	"gopkg.in/yaml.v2"
)

type Car struct {
	Model  string `yaml:"model"`
	Brand  string `yaml:"brand"`
	Prices []int  `yaml:"prices"`
}

func main() {
	var car = []byte(`[ { model: "SLR", brand: "Mercedes", prices: [520, 730], }, { model: "M4", brand: "BMW", prices: [420, 820], } ]`)

	var cars []Car
	if err := yaml.Unmarshal(car, &cars); err != nil {
		panic(err)
	}

	for _, v := range cars {
		fmt.Println(v.Model)
	}

}

输出结果:

SLR
M4
英文:

To unmarshal your input and retrieve the model of each car you have to use umarshal method followed by range method,I have reproduced your scenario as follows :

package main

import (
	"fmt"

	"gopkg.in/yaml.v2"
)

type Car struct {
	Model  string `yaml:"model"`
	Brand  string `yaml:"brand"`
	Prices []int  `yaml:"prices"`
}

func main() {
	var car = []byte(`[ { model: "SLR", brand: "Mercedes", prices: [520, 730], }, { model: "M4", brand: "BMW", prices: [420, 820], } ]`)

	var cars []Car
	if err := yaml.Unmarshal(car, &cars); err != nil {
		panic(err)
	}

	for _, v := range cars {
		fmt.Println(v.Model)

	}

} 

Output:

SLR
M4

答案2

得分: 1

你可以尝试使用yaml来处理你的输入:

package main

import (
	"fmt"
	"gopkg.in/yaml.v2"
)

func main() {
    const cars = `[ { model: "SLR", brand: "Mercedes", prices: [520, 730], }, { model: "M4", brand: "BMW", prices: [420, 820], } ]`

	type Car struct {
		Model  string `yaml:"model"`
		Brand  string `yaml:"brand"`
		Prices []int  `yaml:"prices"`
	}

	var carsArr []Car
	if err := yaml.Unmarshal([]byte(cars), &carsArr); err != nil {
		panic(err)
	}

	fmt.Printf("Cars: %+v", carsArr)
    // Cars: [{Model:SLR Brand:Mercedes Prices:[520 730]} {Model:M4 Brand:BMW Prices:[420 820]}]

}

在这里尝试:https://play.golang.org/p/hRQXSes1tGi

英文:

You may have luck using yaml for your input:

package main

import (
	"fmt"
	"gopkg.in/yaml.v2"
)

func main() {
    const cars = `[ { model: "SLR", brand: "Mercedes", prices: [520, 730], }, { model: "M4", brand: "BMW", prices: [420, 820], } ]`

	type Car struct {
		Model  string `yaml:"model"`
		Brand  string `yaml:"brand"`
		Prices []int  `yaml:"prices"`
	}

	var carsArr []Car
	if err := yaml.Unmarshal([]byte(cars), &carsArr); err != nil {
		panic(err)
	}

	fmt.Printf("Cars: %+v", carsArr)
    // Cars: [{Model:SLR Brand:Mercedes Prices:[520 730]} {Model:M4 Brand:BMW Prices:[420 820]}]

}

Try it here: https://play.golang.org/p/hRQXSes1tGi

huangapple
  • 本文由 发表于 2021年6月12日 18:14:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/67947796.html
匿名

发表评论

匿名网友

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

确定