如何在JSON键中使用GO中的switch?

Here is an example of POST request body:

{"action":"do_something","id":"001"}

I took an example of simple json parser

package main

import (
    "encoding/json"
    "fmt"
    "net/http"
)

type some_json struct {
    Action string `json:"action"`
    Id string `json:"id"`

}

func jsonparse(rw http.ResponseWriter, request *http.Request) {
    decoder := json.NewDecoder(request.Body)

    var post_data some_json
    err := decoder.Decode(&post_data)

    if err != nil {
        panic(err)
    }


    switch ***WHAT_SHOULD_BE_HERE???*** {
    default :
      fmt.Fprintf(w,"WRONG PARAM")
    case "some_thing":
      fmt.Fprintf(w,post_data.Id + "

")
            }
}

func main() {
    http.HandleFunc("/", jsonparse)
    http.ListenAndServe(":8080", nil)
}

I already know how to switch cases from form values, but how to switch cases of json key values?

I am not sure what you want to switch, but i think that you just need to erase the () so Action is not a function call anymore

Be careful maybe your error is that u mixed up the strings in JSON: "do_something" in case: "some_thing"

You can copy following code to playground

package main

import (
    "encoding/json"
    "fmt"
    "strings"
)

type some_json struct {
    Action string `json:"action"`
    Id     string `json:"id"`
}

func jsonparse() {
    r := strings.NewReader("{\"action\":\"do_something\",\"id\":\"001\"}")
    decoder := json.NewDecoder(r)

    var post_data some_json
    err := decoder.Decode(&post_data)

    if err != nil {
        panic(err)
    }

    switch post_data.Action {
    default:
        fmt.Println( "WRONG PARAM")
    case "do_something":
        fmt.Println( post_data.Id+"

")
    }
}

func main() {
    jsonparse()
}