public MJson(string json) { val = json; if (json == null) { return; } try { if (json[0] == '[') { handleArray(json); return; } int length = json.Length; for (int i = 0; i < length; i++) { bool isNext = false; while (i < length && json[i++] != '"') { ; } while (i < length && json[i] != '"') { key.Append(json[i++]); } while (i < length && json[i++] != ':') { ; } while (i < length && json[i] != '"' && json[i] != '[' && json[i] != '{') { if (json[i] == 'n' && json[i + 1] == 'u' && json[i + 2] == 'l' && json[i + 3] == 'l') { dic.Add(key.ToString(), null); key.Clear(); value.Clear(); i += 3; isNext = true; break; } if (json[i] >= '0' && json[i] <= '9') { while (json[i] != ',' && json[i] != '}') { value.Append(json[i++]); } i--; dic.Add(key.ToString(), new MJson(value.ToString().Trim())); key.Clear(); value.Clear(); isNext = true; break; } /////////////////////////////////////////////////////////// i++; } if (isNext) { continue; } i++; if (i - 1 > length || i > length) { return; } if (json[i - 1] == '"') { while (i < length && json[i] != '"') { value.Append(json[i++]); } dic.Add(key.ToString(), new MJson(value.ToString(), true)); key.Clear(); value.Clear(); } else if (json[i - 1] == '{') { value.Append("{"); while ( (i < length && json[i] != '}' && isStart == false) || (i < length && json[i] == '}' && isStart == false && big != 0) || (i < length && isStart)) { if (isStart == false && json[i] == '{') { big++; } if (isStart == false && json[i] == '}') { big--; } value.Append(json[i++]); } value.Append("}"); MJson mj = new MJson(value.ToString()); value.Clear(); dic.Add(key.ToString(), mj); key.Clear(); value.Clear(); } else { value.Append("["); while ( (i < length && json[i] != ']' && isStart == false) || (i < length && json[i] == ']' && isStart == false && middle != 0) || (i < length && isStart)) { if (isStart == false && json[i] == '[') { middle++; } if (isStart == false && json[i] == ']') { middle--; } value.Append(json[i++]); } value.Append("]"); MJson mj = new MJson(value.ToString()); value.Clear(); dic.Add(key.ToString(), mj); key.Clear(); value.Clear(); } } } catch (Exception e) { error = e.ToString(); } }
//获取网络数据-方式2 public static MJson Post(Dictionary <string, string> pairs, string key, out string error) { Log.AddLog("TEST", "4"); string url = Config.net_dic("basePath") + Config.net_dic(key); clearParameter(); bool isFirst = true; foreach (KeyValuePair <string, string> kv in pairs) { if (isFirst) { AddFirstParameter(kv.Key, kv.Value); isFirst = false; } else { addParameter(kv.Key, kv.Value); } } addParameterEnd(); string jsonParas = data.ToString(); data.Clear(); Log.AddLog("Post", "URL:" + url); Log.AddLog("Post", "Ins:" + jsonParas); error = null; try { string strURL = url; //创建一个HTTP请求 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL); //Post请求方式 request.Method = "POST"; //内容类型 request.ContentType = "application/json"; //设置参数,并进行URL编码 string paraUrlCoded = jsonParas;//System.Web.HttpUtility.UrlEncode(jsonParas); byte[] payload; //将Json字符串转化为字节 payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded); //设置请求的ContentLength request.ContentLength = payload.Length; //发送请求,获得请求流 Stream writer; try { writer = request.GetRequestStream();//获取用于写入请求数据的Stream对象 } catch (Exception) { writer = null; Log.AddLog("Post", "Ret:连接服务器失败!"); error = "连接服务器失败"; return(null); } //将请求参数写入流 writer.Write(payload, 0, payload.Length); writer.Close();//关闭请求流 //String strValue = "";//strValue为http响应所返回的字符流 HttpWebResponse response; try { //获得响应流 response = (HttpWebResponse)request.GetResponse(); } catch (WebException ex) { response = ex.Response as HttpWebResponse; } Stream s = response.GetResponseStream(); StreamReader sRead = new StreamReader(s); string postContent = sRead.ReadToEnd(); sRead.Close(); Log.AddLog("Post", "Ret:" + postContent); MJson json = new MJson(postContent); if (json.error != null) { error = json.error; } else if (json["result"].ToString() != "0") { error = json["message"].ToString(); } return(json); } catch (Exception e) { error = e.ToString(); Log.AddLog("Post", error); } return(null); }