public Dictionary <string, string> RequestParser(object[][] oData, REQ_TYPE reqType) { Dictionary <string, string> cb = new Dictionary <string, string>(); // data name, isRequired or not foreach (object[] data in oData) { if (reqType == REQ_TYPE.QUERY) { if (Request.Query.TryGetValue(data[0].ToString(), out StringValues val)) { cb.Add(data[0].ToString(), val.ToString()); } else if ((bool)data[1] == false) { cb.Add(data[0].ToString(), null); } else { return(null); } } else if (reqType == REQ_TYPE.FORM_DATA) { if (Request.Form.TryGetValue(data[0].ToString(), out StringValues val)) { cb.Add(data[0].ToString(), val.ToString()); } else if ((bool)data[1] == false) { cb.Add(data[0].ToString(), null); } else { return(null); } } } return(cb); }
/// <summary> /// ReadJson方法定义的是Json.Net用来处理反序列化的逻辑 /// </summary> public override object ReadJson(JsonReader reader, Type objectType, Object existingValue, JsonSerializer serializer) { BaseREQ result = null; JObject jObject = null; try { jObject = JObject.Load(reader); } catch { return(result); } REQ_TYPE t = REQ_TYPE.NONE; try { Enum.TryParse <REQ_TYPE>(jObject["t"] == null ? "" : jObject["t"].ToString(), out t); if (t == REQ_TYPE.NONE) { return(result); } switch (t) { case REQ_TYPE.CONN_RESP: { String strJson = JsonConvert.SerializeObject(jObject); result = JsonConvert.DeserializeObject <ConnRESP>(strJson); } break; case REQ_TYPE.PUSH_ACK: { String strJson = JsonConvert.SerializeObject(jObject); result = JsonConvert.DeserializeObject <PushAck>(strJson); } break; case REQ_TYPE.CMD_REQ: { String strJson = JsonConvert.SerializeObject(jObject); result = JsonConvert.DeserializeObject <CmdReq>(strJson); } break; } } finally { //以上的一些字段的类型解析错误会为空,则根据t初始BaseREQ类型 if (result == null) { result = new BaseREQ() { t = t } } ; } return(result); } }