O
O
onlinejunior2020-05-16 00:10:30
go
onlinejunior, 2020-05-16 00:10:30

How to put comma in json?

For example, I want to make the structure identical to the VK answer:
[{...},{...},{...},{...}]

Every time I add data, they are not separated by a comma.

JSON, _ := json.Marshal(userVar1)
fmt.Println(string(JSON))
[{...}{...}{...}{...}]

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
onlinejunior, 2020-05-16
@onlinejunior

package main

import (
  "encoding/json"
  "fmt"
)

type Responses struct {
  ID         int     `json:"id"`
  Name       string  `json:"name"`}

func main() {

  var jsonByte = []byte(`[{"ID": 1, "Name": "Пашка"}]`)

  var respos []Responses

  json.Unmarshal(jsonByte, &respos)

  for j := 0; j <= 20; j++ {

    respos = append(respos, Responses{ID: j, Name: "Решил"})
  }

  result, _ := json.Marshal(respos)

  fmt.Println(string(result))
}

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

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question