将URL中的数据转成Json格式并插入数据库



一、下载Newtonsoft.Json类库:
        网址:http://json.codeplex.com/
二、添加引用:
        将URL中的数据转成Json格式并插入数据库_第1张图片
三、引入命名空间:
     using Newtonsoft.Json;
     using Newtonsoft.Json.Linq;
四、编写一个专门处理的类:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace json
{
    class HttpUitls
    {

        public static string Get(string Url)
        {
            //System.GC.Collect();
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
            request.Proxy = null;
            request.KeepAlive = false;
            request.Method = "GET";
            request.ContentType = "application/json; charset=UTF-8";
            request.AutomaticDecompression = DecompressionMethods.GZip;

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
            string retString = myStreamReader.ReadToEnd();

            myStreamReader.Close();
            myResponseStream.Close();

            if (response != null)
            {
                response.Close();
            }
            if (request != null)
            {
                request.Abort();
            }

            return retString;
        }

        public static string Post(string Url, string Data, string Referer) 
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
            request.Method = "POST";
            request.Referer = Referer;
            byte[] bytes = Encoding.UTF8.GetBytes(Data);
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = bytes.Length;
            Stream myResponseStream = request.GetRequestStream();
            myResponseStream.Write(bytes, 0, bytes.Length);

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader myStreamReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            string retString = myStreamReader.ReadToEnd();

            myStreamReader.Close();
            myResponseStream.Close();

            if (response != null)
            {
                response.Close();
            }
            if (request != null)
            {
                request.Abort();
            }
            return retString;
        }


    }
}
五、调用:
        static void Main(string[] args)
        {
            DateTime now = DateTime.Now;

            int key = 0;
            string url = ""; //输入url

            string contenttype = "application/x-www-form-urlencoded";
            string getjson = HttpUitls.Post(url, string.Format("ss={0}&offset={1}&limit={2}&type={3}", key, "0", "10", "1"), contenttype);

            JObject jo = JObject.Parse(getjson);

            string result = jo["data"].ToString();   //选择要插入的键

            
            SaveInfo(result,now);

            Console.WriteLine(result);

            Console.ReadKey();


        }

        private static void SaveInfo(string jsonstr, DateTime now)
        {
            
            string strNow = now.ToString("yyyy-MM-dd HH:mm:ss");
            int count = 0;

            var mJObj = JArray.Parse(jsonstr);

            using (DataClasses1DataContext tt = new DataClasses1DataContext())
            {
                foreach (var item in mJObj)
                {
                    count++;
                    try
                    {
                        Refresh oo = new Refresh();

                        oo.id = count;
                        oo.time = strNow;
                        oo.netName = Convert.ToString(((JObject)item)["netName"]);     //选择对应的字段
                        oo.status = Convert.ToString(((JObject)item)["netStatus"]);
                        tt.Refresh.InsertOnSubmit(oo);
                        tt.SubmitChanges();

                    }
                    catch (Exception)
                    {

                        throw;
                    }

                }
                
            
            }


        }



你可能感兴趣的:(Json)