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();

转载于:https://www.cnblogs.com/jeib/p/4065850.html

你可能感兴趣的:(C# POST提交以及 解析 JSON 实例)