参考资料
C# Json序列化工具--Newtonsoft.Json简介和使用 https://blog.csdn.net/u011127019/article/details/51706619
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.IO;
using System.IO.Compression;
using Newtonsoft.Json.Linq;
using System.IO.Pipes;
using Newtonsoft.Json;
namespace ConsoleApplication2
{
class Program
{
//写文件
static string WriteFile(string data, string fileName, DateTime time)
{
string path = AppDomain.CurrentDomain.BaseDirectory + @"Pay" + time.ToString("yyyy-MM-dd") + @"";
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
//string fileFullPath = path + fileName + ".html";
string fileFullPath = fileName + ".json";
//把 Stream 转换成 byte[]
FileStream fs = new FileStream(fileFullPath, FileMode.Create);
//byte[] bytes = new byte[stream.Length];
fs.Seek(0, SeekOrigin.Begin);
fs.SetLength(0);
//Encoding encode = System.Text.Encoding.GetEncoding("UTF-8");
Encoding encode = Encoding.ASCII;
//Encoding encode = Encoding.Unicode;
StreamWriter sw = new StreamWriter(fs, encode);
sw.Write(data);
sw.Flush();
sw.Close();
fs.Close();
//设置当前流的位置为流的开始
//fs.Seek(0, SeekOrigin.Begin);
//把 byte[] 写入文件
//BinaryWriter bw = new BinaryWriter(fs);
//bw.Write(bytes);
//bw.Close();
//fs.Close();
return fileFullPath;
}
static void Main(string[] args)
{
//Class1 test = new Class1();
string url = "https://www.7366h.com/SportsFt/resultData";
string postdata = "queryDatetime=2019-08-13&page=1&__RequestVerificationToken=Up5n6T1jQuFXWQFljh85e_4s3nNfeJZJHvNK3vm5cxHWQ_eTrZ5963mpUqDY3xl6v8MQjSBvdU2fBVi7kejtfN2l2RZjWgl0Zr22QIyUpzPwQM1GzAggbyNEutgchXjFzCkPdcrJoVK1_6zaXS9fO1t-NArJAOgAstD0kZtBpQ81";
string cookies = "ASP.NET_SessionId=lliszh3bypgbygmafpjlrhz5; __RequestVerificationToken=QNFr4sniyRkUacXuUceOAyINcMZl2CCRyetng8vp9Q70davZmbYKp09EzzQArJzHhxbNh89UWLZ-3JUduorBhMl9PbLR7tvpL31JVuI00zNLd-eR9heHfuzAO09zVkUmxzgnsMh7IFvXSO9eInr9DQ2";
string data = Class1.POST_HTML(url, postdata, cookies);
List<Class1.ResultModel> List_RM = new List<Class1.ResultModel>();
List_RM = Class1.CleanData(data);
for (int i = 0; i < List_RM.Count; i++)
{
string score = List_RM[i].HomeScore_All;
}
string json1 = JsonConvert.SerializeObject(List_RM);
WriteFile(json1, "resultData", DateTime.Now);
//string filePath = WriteFile(json1, "resultData", DateTime.Now);
//var data_dynamic = JsonConvert.DeserializeObject<dynamic>(json1);
}
}
}
如果写的文件有乱码,换默认编码
Encoding encode = Encoding.Default;