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

发布于 2025-01-11 13:19:34 字数 3025 浏览 0 评论 0原文

我有一个 struct 如下:

type TourData struct {
    ArtistID   int    //artist ID
    RelationID string //key for relations
    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"
                ]
            }
        }

日期来自另一个结构。我用来填充这个结构的代码如下:

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, &TourThings)

我需要将所有 1 组合在一起,然后是 2 等。我想创建另一个结构,并从这个结构中填充,但运气不佳 - 有什么想法吗?

为了澄清,我想说 TourData.City 等于:

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

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

我尝试创建另一个结构体,从 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、帕皮提]。

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:"id"`
    DatesLocations map[string][]string `json:"datesLocations"`
}

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:

"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"
                ]
            }
        }

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, &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 < 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)
    }

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十雾 2025-01-18 13:19:34

如果我正确理解您的要求,您还需要将 city 声明为字符串数组。 (还有与之相配的国家/地区)。

查看此解决方案: 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文