首頁  >  文章  >  後端開發  >  將 json 映射為具有嵌套字典的結構

將 json 映射為具有嵌套字典的結構

WBOY
WBOY轉載
2024-02-09 13:46:231096瀏覽

将 json 映射为具有嵌套字典的结构

php小編草莓將json映射為具有嵌套字典的結構是一種常見的資料處理方法。透過將json資料轉換為嵌套字典,我們可以更方便地對資料進行操作和存取。嵌套字典的結構可以提供更靈活的資料組織方式,使我們能夠更有效率地處理複雜的資料結構。在實際應用中,將json映射為嵌套字典可以幫助我們更好地理解和處理數據,提高程式碼可讀性和維護性。無論是處理API返回的json數據,還是解析配置文件,將json映射為嵌套字典都是一種常見的數據處理技巧。

問題內容

我是 golang 新手。我有一個帶有嵌套結構的 json 文件,我想解析和填充它。

我正在嘗試使用地圖結構來嘗試填充。我能夠對簡單的結構做到這一點。但是當涉及到字典數組(key:struct)時。 map[string]介面{} 似乎會因為 runtime 錯誤而失敗:索引超出範圍

我嘗試對下面的 json 範例執行以下操作。

type window struct {
    loc    []int
    wrtc   string
    label  string
}

type view struct {
    windows   []window
}

type views struct {
    views []view
}

type desktop struct {
    views                 []views  `mapstructure:views`
    rotation_speed        string   `mapstructure:"rotationspeed" json:rotationspeed"`
}

func main() {
        file, _ := ioutil.readfile("test.json")

        data := desktop{}

        _ = json.unmarshal([]byte(file), &data)

        fmt.println("data: ", data.views[0])
}
{
"desktop": {
    "view": [{// configs for view1
                 "random_id1": {
                         "loc": [0,0,640,360],
                         "wrtc": "some string",
                         "label": "window 1"
                 },
                 "random_id213443": {
                         "loc": [640,360,1280,720],
                         "wrtc": "some string blah",
                         "label": "window 2"
                 },
                 // more windows with random ids....
              },
              {
               // configs for view2...
              }
             ],
    "rotationSpeed": 30
}

由於視窗 id 是隨機的,我無法在結構中定義它。 我嘗試使用 mapstruct:",squash" 但這似乎也失敗了。

非常感謝您提供的任何幫助。

解決方法

@burak serdar 是對的

您不需要地圖結構。 json 解組可以解決這個問題。

你的程式碼有很多錯誤,像是結構、大寫、「視圖」等。

以下是示範:

package main

import (
    "encoding/json"
    "fmt"
)

var data = `
{
    "desktop":{
        "view":[
            {
                "random_id1_1":{
                    "loc":[
                        0,
                        0,
                        640,
                        360
                    ],
                    "wrtc":"some string",
                    "label":"window 1"
                },
                "random_id1_2":{
                    "loc":[
                        640,
                        360,
                        1280,
                        720
                    ],
                    "wrtc":"some string blah",
                    "label":"window 2"
                }
            },
            {
                "random_id2_1":{
                    "loc":[
                        0,
                        0,
                        640,
                        360
                    ],
                    "wrtc":"some string",
                    "label":"window 1"
                },
                "random_id2_2":{
                    "loc":[
                        640,
                        360,
                        1280,
                        720
                    ],
                    "wrtc":"some string blah",
                    "label":"window 2"
                }
            }
        ],
        "rotationspeed":30
    }
}
`

type window struct {
    loc   []int
    wrtc  string
    label string
}

type desktop struct {
    view           []map[string]window
    rotation_speed int `json:"rotationspeed" mapstructure:"rotationspeed"`
}

type config struct {
    desktop desktop
}

func main() {
    c := config{}
    json.unmarshal([]byte(data), &c)
    fmt.println("json.unmarshal: ", c)
}
json.unmarshal:  {{[map[random_id1_1:{[0 0 640 360] some string window 1} random_id1_2:{[640 360 1280 720] some s
tring blah window 2}] map[random_id2_1:{[0 0 640 360] some string window 1} random_id2_2:{[640 360 1280 720] some
 string blah window 2}]] 30}}

如果你想要 view 結構,你也可以透過「remain」使用mapstruct

type Window struct {
    Loc   []int
    Wrtc  string
    Label string
}

type View struct {
    Windows map[string]Window `mapstructure:",remain"`
}

type Desktop struct {
    View           []View
    Rotation_speed int `json:"rotationSpeed" mapstructure:"rotationSpeed"`
}

type Config struct {
    Desktop Desktop
}

func main() {
    c2 := Config{}
    m := map[string]interface{}{}
    _ = json.Unmarshal([]byte(data), &m)
    mapstructure.Decode(m, &c2)
    fmt.Println("mapstructure: ", c2)
}

以上是將 json 映射為具有嵌套字典的結構的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:stackoverflow.com。如有侵權,請聯絡admin@php.cn刪除