亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用給定數據創建 JSON 數據作為 map[string] 接口

使用給定數據創建 JSON 數據作為 map[string] 接口

Go
幕布斯7119047 2023-07-26 13:39:45
以下是從接收到的數據{  "details": [    {      "attdetails": [        {          "id": "a48c8539-caaf-49a5-9346-8e88e60e7af4",          "name": "compute01"        },        {          "id": "a48c8539-caaf-49a5-9346-8e88e60e7af4",          "name": "compute02"        }      ],      "name": "item1"    },    {      "attdetails": [        {          "id": "85bdafa7-274e-4180-b76f-12f390a274fc",          "name": "compute03"        },        {          "id": "85bdafa7-274e-4180-b76f-12f390a274fc",          "name": "compute04"        }      ],      "name": "item1"    }  ]}我正在嘗試使用以下數據創建 JSON:["item1":   {"compute01": "a48c8539-caaf-49a5-9346-8e88e60e7af4"},   {"compute02": "a48c8539-caaf-49a5-9346-8e88e60e7af4"},    {"compute03": "a48c8539-caaf-49a5-9346-8e88e60e7af4"},    {"compute04": "a48c8539-caaf-49a5-9346-8e88e60e7af4"}]我嘗試創建映射[字符串]接口,但我無法將它們分組在一起并將它們添加到數組中到特定的鍵。以下是我正在嘗試使用的示例代碼:var childrens map[string]interface{}for _, a := range atts {    att, ok := childrens[a.Name]    if !ok {        childrens[a.Name] = make([]map[string]string, 0)    }    var c map[string]string    for _, each := range a.Details {        c[each.Name] = each.Value    }    childrens[a.Name] = append(c, childrens[a.Name])}
查看完整描述

3 回答

?
12345678_0001

TA貢獻1802條經驗 獲得超5個贊

我認為像下面這樣的東西應該可以解決問題,

https://play.golang.org/p/YbZ1niXyFBR

它并不完全是您想要的結構,但應該為您提供調整方向的要點。


查看完整回答
反對 回復 2023-07-26
?
當年話下

TA貢獻1890條經驗 獲得超9個贊

這是另一種選擇。


package main


import (

    "encoding/json"

    "fmt"

)


var raw = `

{

"details": [

    {

    "attdetails": [

        {

        "id": "a48c8539-caaf-49a5-9346-8e88e60e7af4",

        "name": "compute01"

        },

        {

        "id": "a48c8539-caaf-49a5-9346-8e88e60e7af4",

        "name": "compute02"


        }

    ],

    "name": "item1"

    },

    {

    "attdetails": [

        {

        "id": "85bdafa7-274e-4180-b76f-12f390a274fc",

        "name": "compute03"

        },

        {

        "id": "85bdafa7-274e-4180-b76f-12f390a274fc",

        "name": "compute04"

        }

    ],

    "name": "item1"

    }

]

}

`


type Data struct {

    Details []struct {

        AttDetails []struct {

            Id   string `json:"id"`

            Name string `json:"name"`

        } `json:"attdetails"`

        Name string `json:"name"`

    } `json:"details"`

}


func main() {

    var data Data


    err := json.Unmarshal([]byte(raw), &data)

    if err != nil {

        fmt.Println(err)

    }


    output := map[string][]map[string]string{}

    for _, detail := range data.Details {

        for _, attdetail := range detail.AttDetails {

            output[detail.Name] = append(output[detail.Name], map[string]string{

                attdetail.Name: attdetail.Id,

            })

        }

    }


    // b, _ := json.Marshal(output) // non-pretty version

    b, _ := json.MarshalIndent(output, "", "\t")

    fmt.Println(string(b))

}


查看完整回答
反對 回復 2023-07-26
?
胡說叔叔

TA貢獻1804條經驗 獲得超8個贊

func jsonToMap(jsonStr string) map[string]interface{} {

    result := make(map[string]interface{})

    json.Unmarshal([]byte(jsonStr), &result)

    return result

}

示例 - https://goplay.space/#ra7Gv8A5Heh


查看完整回答
反對 回復 2023-07-26
?
犯罪嫌疑人X

TA貢獻2080條經驗 獲得超4個贊

我能夠使用以下代碼獲得所需的結構:


var cs = make(map[string][]map[string]string)



for _, a := range atts{

    _, ok := childrens[a.Name]

    if !ok {

        cs[a.Name] = make([]map[string]string, 0)

    }

    var c = make(map[string]string)

    for _, each := range a.Details {

        c[each.Name] = each.Value

    }

    cs[a.Name] = append(cs[a.ClassName], c)

}


查看完整回答
反對 回復 2023-07-26
  • 3 回答
  • 0 關注
  • 212 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號