第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何將標(biāo)頭添加到 JSON 以識(shí)別數(shù)組值的數(shù)組名稱

如何將標(biāo)頭添加到 JSON 以識(shí)別數(shù)組值的數(shù)組名稱

Go
湖上湖 2023-04-24 16:12:14
我正在嘗試查看是否有一種方法(簡(jiǎn)單方法)可以使用encoding/jsonGO 向 JSON 中的每個(gè)數(shù)組添加標(biāo)頭。我的意思是說?想要有這樣的東西:{     "Dog":[      {           "breed":"Chihuahua",           "color":"brown"      },      {           "breed":"Pug",           "color":"white"      }    ],     "Cat":[     {           "breed":"British",           "color":"white"     },           "breed":"Ragdoll",           "color":"gray"     }    ]}主要思想是在這種情況下有一個(gè)“類別”Dog和Cat。我已經(jīng)有了這個(gè)解決方案,但我正在尋找可以改進(jìn)它的東西。我的代碼如下所示:type Dog struct {   Breed string   Color string}type Cat struct {   Breed string   Color string}func main(){   dogs := [...]Dog{       {"Chihuahua", "brown"},       {"Pug", "white"},   }   cats := [...]Cat{        {"British", "white"},        {"Ragdoll", "gray"},   }   c, err := json.MarshalIndent(cats, "", "\t")   if err != nil {       log.Fatal(err)   }   d, err := json.MarshalIndent(dogs, "", "\t")   if err != nil {      log.Fatal(err)   }   fmt.Println("{")   fmt.Printf("    \"Dog\": %v,\n", string(d))   fmt.Printf("    \"Cat\": %v\n}", string(c))}主要想法是將“狗”和“貓”作為新數(shù)組,但我想改進(jìn)我的代碼,不要讓它看起來(lái)像應(yīng)該的那樣“硬編碼”,我想知道是否有一種簡(jiǎn)單的方法添加標(biāo)題“Dog”和所有數(shù)組值,添加標(biāo)題“Cat”和所有數(shù)組值。
查看完整描述

1 回答

?
慕標(biāo)琳琳

TA貢獻(xiàn)1830條經(jīng)驗(yàn) 獲得超9個(gè)贊

不需要為狗和貓分別創(chuàng)建json對(duì)象。這將導(dǎo)致在編組數(shù)據(jù)時(shí)產(chǎn)生單獨(dú)的 json 對(duì)象。


您嘗試的方法基本上是適當(dāng)且無(wú)用的。


方法應(yīng)該創(chuàng)建一個(gè)結(jié)果結(jié)構(gòu),它將 dogs 和 cats 結(jié)構(gòu)作為字段,類型分別作為它們的切片。舉個(gè)例子:


package main


import (

? ? "fmt"

? ? "encoding/json"

? ? "log"

)


type Result struct{

? ? Dog []Dog

? ? Cat []Cat

}


type Dog struct{

? ?Breed string

? ?Color string

}


type Cat struct {

? ?Breed string

? ?Color string

}


func main(){


? ?dogs := []Dog{

? ? ? ?{"Chihuahua", "brown"},

? ? ? ?{"Pug", "white"},

? ?}


? ?cats := []Cat{

? ? ? ? {"British", "white"},

? ? ? ? {"Ragdoll", "gray"},

? ?}


? ?result := Result{

? ? Dog: dogs,

? ? Cat: cats,

? ?}?


? ?output, err := json.MarshalIndent(result, "", "\t")

? ?if err != nil {

? ? ? ?log.Fatal(err)

? ?}

? ?fmt.Println(string(output))


}

輸出:


{

? ? "Dog": [

? ? ? ? {

? ? ? ? ? ? "Breed": "Chihuahua",

? ? ? ? ? ? "Color": "brown"

? ? ? ? },

? ? ? ? {

? ? ? ? ? ? "Breed": "Pug",

? ? ? ? ? ? "Color": "white"

? ? ? ? }

? ? ],

? ? "Cat": [

? ? ? ? {

? ? ? ? ? ? "Breed": "British",

? ? ? ? ? ? "Color": "white"

? ? ? ? },

? ? ? ? {

? ? ? ? ? ? "Breed": "Ragdoll",

? ? ? ? ? ? "Color": "gray"

? ? ? ? }

? ? ]

}


查看完整回答
反對(duì) 回復(fù) 2023-04-24
  • 1 回答
  • 0 關(guān)注
  • 120 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)