私の websocket サーバーは、JSON データを受信して非整列化します。このデータは常に、キーと値のペアを持つオブジェクトにラップされます。キー文字列は値の識別子として機能し、Go サーバーにその値の種類を伝えます。値の型がわかれば、JSON で値を正しい型の構造体に非整列化することができます。
各 json-object には、複数のキーと値のペアが含まれる場合があります。
JSON の例:
{
"sendMsg":{"user":"ANisus","msg":"Trying to send a message"},
"say":"Hello"
}
"encoding/json"
パッケージを使用してこれを行う簡単な方法はありますか?
package main
import (
"encoding/json"
"fmt"
)
// the struct for the value of a "sendMsg"-command
type sendMsg struct {
user string
msg string
}
// The type for the value of a "say"-command
type say string
func main(){
data := []byte(`{"sendMsg":{"user":"ANisus","msg":"Trying to send a message"},"say":"Hello"}`)
// This won't work because json.MapObject([]byte) doesn't exist
objmap, err := json.MapObject(data)
// This is what I wish the objmap to contain
//var objmap = map[string][]byte {
// "sendMsg": []byte(`{"user":"ANisus","msg":"Trying to send a message"}`),
// "say": []byte(`"hello"`),
//}
fmt.Printf("%v", objmap)
}
あらゆる種類の提案/ヘルプをありがとう!