O
O
Oleg Voitenko2018-04-29 15:56:03
go
Oleg Voitenko, 2018-04-29 15:56:03

How to form a structure from multilevel json in go?

tell me how to form a structure from a multi-level json on go: That is, I'm trying to do it like this:
[{ status: 200, body: { user: 'Bravis' }]

jsonResp := map[string]string {
                        "status" : "200",
                        },
                        body : map[string]string {
                            "user" : u.Value,
                            "hand"  : "Right",
                        },
                     }


                     jsonResponse, _ := json.Marshal(jsonResp)
                     fmt.Fprintf(w, string(jsonResponse))

But this trick doesn't work.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artem Kislenko, 2018-04-29
@OliverV

package main

import (
  "fmt"
  "encoding/json"
)

func main() {
  jsonResp := map[string]interface {}{"status" : "200", "body": map[string]interface {} {"user": "s", "dd": "dd"}}
  jsonResponse, _ := json.Marshal(jsonResp)
   fmt.Println(string(jsonResponse))
}

https://play.golang.org/p/OWCjJfxPBao
But it's better to use structs

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question