初次做长连接服务,遇到点小问题,以前都是http的,以url确定服务
举个例子说得清楚点 比如有2个服务,一个登录一个聊天,客户端传过来对应的json是
登录: {"type":"login","username":"abc","password":"123456"} 聊天: {"type":"chat","from":"张三","to":"李四","content":"李四你好"} json的type字段就表明了请求什么服务
对于golang通常我们要把这俩json解析为对应的结构体:
type Login struct{
Type string
Username string
Password string
}
type Chat struct{
Type string
From string
To string
Content string
}
问题就是我在刚收到json字符串还不知道type是什么情况下怎么确定把该json解析到相应的结构体?我该 json.Unmarshal(json,?)
矛盾在于 这个type要解析过后才知道,然而解析的时候还不知道type我不知道该解析为哪个结构体 或者说我一开始的设计思路就是错的?