登录
首页 >  Golang >  Go问答

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

来源:stackoverflow

时间:2024-04-11 14:51:39 304浏览 收藏

一分耕耘,一分收获!既然打开了这篇文章《如何通过名称获取特定 api,然后从这个“Apis”结构列表中获取它的 ID?》,就坚持看下去吧!文中内容包含等等知识点...希望你能在阅读本文后,能真真实实学到知识或者帮你解决心中的疑惑,也欢迎大佬或者新人朋友们多留言评论,多给建议!谢谢!

问题内容

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 新手,非常感谢一个解释清楚的答案。 谢谢大家


解决方案


在您的情况下, 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),这更好(这是最好的)。

本篇关于《如何通过名称获取特定 api,然后从这个“Apis”结构列表中获取它的 ID?》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于Golang的相关知识,请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>