将结构中的字符串转换为 []string。

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

Convert string in struct to []string

问题

我有一个如下的struct

type TourData struct {
	ArtistID   int    // 艺术家ID
	RelationID string // 关系键
	City       string // 城市
	Country    string // 国家
	TourDates  []string // 巡演日期
}

type MyRelation struct {
	ID             int                 `json:"id"`
	DatesLocations map[string][]string `json:"datesLocations"`
}

其中包含了来自一个csv文件的数据:

1,nagoya-japan,Nagoya,Japan,
1,penrose-new_zealand,Penrose,New_Zealand,
1,dunedin-new_zealand,Dunedin,New_Zealand,
2,playa_del_carmen-mexico,Playa Del Carmen,Mexico,
2,papeete-french_polynesia,Papeete,French_Polynesia,

MyRelations是从一个API中获取的数据,包含了:

"index": [
	{
		"id": 1,
		"datesLocations": {
			"dunedin-new_zealand": [
				"10-02-2020"
			],
			"nagoya-japan": [
				"30-01-2019"
			],
			"penrose-new_zealand": [
				"07-02-2020"
			]
		}
	},
	{
		"id": 2,
		"datesLocations": {
			"papeete-french_polynesia": [
				"16-11-2019"
			],
			"playa_del_carmen-mexico": [
				"05-12-2019",
				"06-12-2019",
				"07-12-2019",
				"08-12-2019",
				"09-12-2019"
			]
		}
	}
]

日期来自另一个struct。我用以下代码来填充这个struct

var oneRecord TourData
var allRecords []TourData

for _, each := range csvData {
	oneRecord.ArtistID, _ = strconv.Atoi(each[0])
	oneRecord.RelationID = each[1]
	oneRecord.City = each[2]
	oneRecord.Country = each[3]
	oneRecord.TourDates = Relations.Index[oneRecord.ArtistID-1].DatesLocations[each[1]]
	allRecords = append(allRecords, oneRecord)
}

jsondata, err := json.Marshal(allRecords) // 转换为JSON
json.Unmarshal(jsondata, &TourThings)

我需要将所有的1分组在一起,然后是所有的2等等。我想创建另一个struct,从这个struct中填充,但是没有太多成功的尝试 - 有什么建议吗?

为了澄清,我希望TourData.City的值为:

[Nagoya, Penrose, Dunedin]
[Playa Del Carmen, Papeete]

目前,如果我打印TourData[0].City,我会得到Nagoya。

我尝试创建另一个struct,从TourData结构中填充,具有以下字段:

type TourDataArrays struct {
	ArtistID  int
	City      []string
	Country   []string
	TourDates [][]string
}

然后使用以下代码填充该结构:

var tourRecord TourDataArrays
var tourRecords []TourDataArrays
for i := 0; i < len(Relations.Index); i++ {
	for j := 0; j < len(allRecords); j++ {
		if allRecords[i].ArtistID == i+1 {
			tourRecord.City = append(tourRecord.City, allRecords[j].City)
		}
	}
	tourRecords = append(tourRecords, tourRecord)
}

然而,这将所有的城市都添加到一个数组中,即:

[Nagoya, Penrose, Dunedin, Playa Del Carmen, Papeete]
英文:

I have a struct as below:

type TourData struct {
 	ArtistID   int    //artist ID
   	RelationID string //key for relations
   	City       string
   	Country    string
   	TourDates  []string
}

type MyRelation struct {
	ID             int                 `json:&quot;id&quot;`
	DatesLocations map[string][]string `json:&quot;datesLocations&quot;`
}

which contains this data from a csv file:

1,nagoya-japan,Nagoya,Japan,
1,penrose-new_zealand,Penrose,New_Zealand,
1,dunedin-new_zealand,Dunedin,New_Zealand,
2,playa_del_carmen-mexico,Playa Del Carmen,Mexico,
2,papeete-french_polynesia,Papeete,French_Polynesia,

MyRelations is populated from an API which contains:

&quot;index&quot;: [
        {
            &quot;id&quot;: 1,
            &quot;datesLocations&quot;: {
                &quot;dunedin-new_zealand&quot;: [
                    &quot;10-02-2020&quot;
                ],
                &quot;nagoya-japan&quot;: [
                    &quot;30-01-2019&quot;
                ],
                &quot;penrose-new_zealand&quot;: [
                    &quot;07-02-2020&quot;
                ]
            }
        },
        {
            &quot;id&quot;: 2,
            &quot;datesLocations&quot;: {
                &quot;papeete-french_polynesia&quot;: [
                    &quot;16-11-2019&quot;
                ],
                &quot;playa_del_carmen-mexico&quot;: [
                    &quot;05-12-2019&quot;,
                    &quot;06-12-2019&quot;,
                    &quot;07-12-2019&quot;,
                    &quot;08-12-2019&quot;,
                    &quot;09-12-2019&quot;
                ]
            }
        }

The dates come from another struct. The code I have used to populate this struct is as below:

var oneRecord TourData
	var allRecords []TourData

for _, each := range csvData {
	oneRecord.ArtistID, _ = strconv.Atoi(each[0])
	oneRecord.RelationID = each[1]
	oneRecord.City = each[2]
	oneRecord.Country = each[3]
	oneRecord.TourDates = Relations.Index[oneRecord.ArtistID-1].DatesLocations[each[1]]
	allRecords = append(allRecords, oneRecord)
}
    
jsondata, err := json.Marshal(allRecords) // convert to JSON
json.Unmarshal(jsondata, &amp;TourThings)

I need to group all the 1s together then the 2s etc. I thought to create another struct, and populate from this one but not having much luck - any ideas?

To clarify I would want say TourData.City to equal:

[Nagoya,Penrose,Dunedin]
[Playa Del Carmen, Papeete]

At the moment if I was to print TourData[0].City I would get Nagoya.

I have tried creating another struct to be populated from the TourData struct with the following fields:

type TourDataArrays struct {
	ArtistID  int
	City      []string
	Country   []string
	TourDates [][]string
}

and then populate the struct using the code below:

var tourRecord TourDataArrays
	var tourRecords []TourDataArrays
	for i := 0; i &lt; len(Relations.Index); i++ {
		for j := 0; j &lt; len(allRecords); j++ {
			if allRecords[i].ArtistID == i+1 {
				tourRecord.City = append(tourRecord.City, allRecords[j].City)
			}
		}
		tourRecords = append(tourRecords, tourRecord)
	}

However this is adding all the cities to one array i.e

[Nagoya, Penrose, Dunedin, Playa Del Carmen, Papeete].

答案1

得分: 1

如果我正确理解您的要求,您还需要将城市声明为字符串数组(并且还需要国家)。请查看以下解决方案:https://go.dev/play/p/osgkbfWV3c5

请注意,我没有对国家进行去重,并且从Json中的一个字段派生了城市和国家。

英文:

If I understand your requirements correctly you needed to declare city as a string array as well. (And Country to go with it).

Check out this solution : https://go.dev/play/p/osgkbfWV3c5

Note I have not deduped country and derived city and country from one field in the Json.

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

发表评论

匿名网友

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

确定