C# POST提交以及 解析 JSON 实例

一、解析的JSON字符串如下

{"tinyurl":"http:\/\/dwz.cn\/v9BxE","status":0,"longurl":"http://mp.weixin.qq.com/s?__biz=MzAxODA3NTc1OQ==","err_msg":""}

二、操作前先下载JSON.NET 

三、添加引用 Newtonsoft.Json 

四、引入命名空间

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

五、post提交及JSON解析

string postDataStr = urlText;
string Url = "http://dwz.cn/create.php";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.ContentType = "application/x-www-form-urlencoded";
request.CookieContainer = new CookieContainer();
request.Method = "POST";
request.ContentLength = postDataStr.Length;
Stream myRequestStream = request.GetRequestStream();
StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));
myStreamWriter.Write(postDataStr);
myStreamWriter.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
string retString = myStreamReader.ReadToEnd();
// string ujson = "[" + retString + "]";
string ujson = retString;
// JArray ja =(JArray) JsonConvert.DeserializeObject(ujson);
// JObject o = (JObject)ja[0];
//JsonReader reader = new JsonTextReader(new StringReader(ujson));
JObject jo = JObject.Parse(retString);
//this.textBox3.Text = o["tinyurl"].ToString();
this.textBox3.Text = jo.Property("tinyurl").Value.ToString();

C# POST提交以及 解析 JSON 实例

上一篇:(转)安装 Apache 出现 以一种访问权限不允许的方式做了一个访问套接字的尝试


下一篇:WinMerge文件编码设置