簡體   English   中英

如何通過 json 將鍵值對數組傳遞給結構體的 golang 切片

[英]How do I pass array of key,value pairs to golang slice of struct through json

我正在寫一個簡單的 post api 請求。 我能夠將 JSON 解析為 golang 結構,直到 peername json 對象。 我不知道通過 api 的 JSON 主體傳遞值來填充結構的 golang 切片的正確語法。

我正在嘗試解析通過 api 發送的 JSON 正文。 這是示例正文請求 -

{  
   "type":"string",
   "name":"string",
   "organization":{  
      "orgID":"1",
      "orgName":"string",
      "peer":{  
         "peerID":"1",
         "peerName":"string"
      },
      "attributes":[
    ["slide0001.html", "Looking Ahead"],
    ["slide0008.html", "Forecast"],
    ["slide0021.html", "Summary"]
]
    }
} "peerName":"string"
          },
          "attributes":["name":"string":"value":true]
        }
    }

這是我的示例 golang 結構。

//Identity ...
type Identity struct {
    Type         string        `json:"type,omitempty"`
    Name         string        `json:"name,omitempty"`
    Organization *Organization `json:"organization,omitempty"`
}

//Organization ....
type Organization struct {
    OrgID      string      `json:"orgID,omitempty"`
    OrgName    string      `json:"orgName,omitempty"`
    Peer       *Peer       `json:"peer,omitempty"`
    Attributes *Attributes `json:"attributes"`
}

//Peer ...
type Peer struct {
    PeerID   string `json:"peerID,omitempty"`
    PeerName string `json:"peerName,omitempty"`
}

//Attributes ...
type Attributes []struct {
    Name  string `json:"name"`
    Value bool   `json:"value"`
}

終於想出了正確的語法。 我們必須通過 JSON 傳遞一個結構數組。

{  
   "type":"string",
   "name":"string",
   "organization":
   {  
      "orgID":"1",
      "orgName":"string",
      "peer":
      {  
         "peerID":"1",
         "peerName":"string"
      },
      "attributes":
      [
        {"slide0001.html": "Looking Ahead"},
        {"slide0008.html": "Forecast"},
        {"slide0021.html": "Summary"}
      ]
    }
}

你可以在UnmarshalJSON函數中做任何你想做的事情。

我在操場上做了一個例子。 https://play.golang.org/p/WY6OCR8K3Co

你可以得到輸出: {A:[{Name:slide0001.html Value:Looking Ahead} {Name:slide0008.html Value:Forecast} {Name:slide0021.html Value:Summary}]}

var (
    jso = []byte(`
    {  
        "attributes":
        [
            {"slide0001.html": "Looking Ahead"},
            {"slide0008.html": "Forecast"},
            {"slide0021.html": "Summary"}
        ]
     }`)
)

type B struct {
    A As `json:"attributes"`
}

type As []A

type A struct {
    Name  string
    Value string
}

func (as *As) UnmarshalJSON(data []byte) error {
    var attr []interface{}
    if err := json.Unmarshal(data, &attr); err != nil {
        return err
    }
    if len(attr) > 0 {
        newAs := make([]A, len(attr))
        // i := 0
        for i, val := range attr {
            if kv, ok := val.(map[string]interface{}); ok && len(kv) > 0 {
                for k, v := range kv {
                    a := A{
                        Name:  k,
                        Value: v.(string),
                    }
                    newAs[i] = a
                    i++
                    break
                }
            }
        }
        *as = newAs
    }
    return nil
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM