2

//如何使用golang映射创建JSON数组?

 3 years ago
source link: https://studygolang.com/articles/35169
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

//如何使用golang映射创建JSON数组?

xiaojun1195 · 大约3小时之前 · 28 次点击 · 预计阅读时间 1 分钟 · 大约8小时之前 开始浏览    
//如何使用golang映射创建JSON数组?
package main

import (
   "encoding/json"
   "fmt"
)

type data struct {
   Id string
   Name string
}

func main(){
   id := [5]string{"1","2","3","4","5"}
   name := [5]string{"A","B","C","D","E"}
   var parsedData []data

   for counter := range id {
      parsedData = append(parsedData, data{Name: name[counter], Id: id[counter]})
   }

   bytes, _ := json.Marshal(parsedData)
   fmt.Print(string(bytes))
}

有疑问加站长微信联系(非本文作者)

280

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK