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

使用给定数据创建 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 关注
  • 109 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信