为了账号安全,请及时绑定邮箱和手机立即绑定

如何通过名称获取特定的 api,然后从此“Apis”结构列表中获取它的 ID?

如何通过名称获取特定的 api,然后从此“Apis”结构列表中获取它的 ID?

Go
交互式爱情 2022-06-27 10:20:12
type Apis struct {    Items []struct {        ID                    string `json:"id"`        Name                  string `json:"name"`        Description           string `json:"description"`        CreatedDate           int    `json:"createdDate"`        APIKeySource          string `json:"apiKeySource"`        EndpointConfiguration struct {            Types []string `json:"types"`        } `json:"endpointConfiguration"`    } `json:"items"`}这是我定义的结构,用于存储我以 json 格式获得的 API。如何通过名称获取特定 API,然后获取其 ID。例如,假设apiname == Shopping我希望将 Shopping API 的 ID 分配给id变量。ps:我是golang的新手,非常感谢解释清楚的答案。多谢你们
查看完整描述

1 回答

?
吃鸡游戏

TA贡献1829条经验 获得超7个赞

在您的情况下Items是自定义结构的切片,因此您必须执行循环搜索,如下所示:


package main


import (

    "encoding/json"

    "fmt"

)


type Apis struct {

    Items []struct {

        ID                    string `json:"id"`

        Name                  string `json:"name"`

        Description           string `json:"description"`

        CreatedDate           int    `json:"createdDate"`

        APIKeySource          string `json:"apiKeySource"`

        EndpointConfiguration struct {

            Types []string `json:"types"`

        } `json:"endpointConfiguration"`

    } `json:"items"`

}


func main() {

    // Some JSON example:

    jsonStr := `{"items": [{"id":"1","name":"foo"},{"id":"2","name":"bar"}]}`


    // Unmarshal from JSON into Apis struct.

    apis := Apis{}

    err := json.Unmarshal([]byte(jsonStr), &apis)

    if err != nil {

        // error handling

    }


    // Actual search:

    nameToFind := "bar"

    for _, item := range apis.Items {

        if item.Name == nameToFind {

            fmt.Printf("found: %+v", item.ID)

            break

        }

    }

}

使用自定义结构而不是切片会更好map,因此您可以执行以下操作:


package main


import (

    "encoding/json"

    "fmt"

)


type Apis struct {

    Items map[string]struct {

        ID                    string `json:"id"`

        Name                  string `json:"name"`

        Description           string `json:"description"`

        CreatedDate           int    `json:"createdDate"`

        APIKeySource          string `json:"apiKeySource"`

        EndpointConfiguration struct {

            Types []string `json:"types"`

        } `json:"endpointConfiguration"`

    } `json:"items"`

}


func main() {

    // Some JSON example:

    jsonStr := `{"items": {"foo":{"id":"1","name":"foo"},"bar":{"id":"2","name":"bar"}}}`


    // Unmarshal from JSON into Apis struct.

    apis := Apis{}

    err := json.Unmarshal([]byte(jsonStr), &apis)

    if err != nil {

        // error handling

    }


    // Actual search:

    nameToFind := "bar"

    item, found := apis.Items[nameToFind]

    if !found {

        fmt.Printf("item not found")

    }

    fmt.Printf("found: %+v", item)

}

重要提示:使用切片,算法的复杂性将O(n)与地图有关 -O(1)这会更好(这是最好的)。


查看完整回答
反对 回复 2022-06-27
  • 1 回答
  • 0 关注
  • 136 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号