Json格式化
Json转C#实体类
在类单元里把int改成了string字符串
提示错误
代码如下:
json已转的C#实体类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WindowsFormsApp3
{
public class Root
{
public List<List<string>> l { get; set; } //原来的int被我修改成string
public List<List<string>> n { get; set; } //原来的int被我修改成string
}
}
using Newtonsoft.Json; //引入这个库
using System.Net; //HttpWebRequest
using System.IO; //Stream流
private void button1_Click(object sender, EventArgs e)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(@"https://phoile.tender88.com/sports-service/sv/odds/events?mk=1&sp=29&ot=2&btg=1&o=1&lg=&ev=&d=&l=100&v=0&me=0&more=false&c=CN&tm=0&g=QQ%3D%3D&pa=0&cl=100&_g=1&pimo=0%2C1%2C8%2C3%2C6%2C7%2C4%2C5&inl=false&_=1671623147061&locale=zh_CN"));
request.Method = "GET"; //请求的方法
request.Accept = "application/json, text/javascript, */*; q=0.01"; //可以接受哪些类型的文件
request.Headers.Add("Accept-Language", "zh-CN,zh;q=0.9"); //头部信息添加语言
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36"; //访问验证
request.ContentType = "application/json;charset=UTF-8"; //识别编码格式UTF-8
HttpWebResponse response = (HttpWebResponse)request.GetResponse(); //响应,强制转换
Stream myResponseStream = response.GetResponseStream(); //获取流,需要引用命名空间IO
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8); //流读取器,观察者模式
//textBox1.Text = myStreamReader.ReadToEnd(); //读取放到text里
//以下代码是json
string getJson = myStreamReader.ReadToEnd();
Root rt = JsonConvert.DeserializeObject<Root>(getJson); //getJson是字符串,反序列化给Root类,我已经在Root类里把int改成了string字符串了.为何还不行?
for (int i = 0; i < rt.l.Count; i++) //由于public List<List<string>> l { get; set; } l是一个集合,所以需要遍历集合里面的所有数据
{
textBox1.Text = ("A=" + rt.l);
//MessageBox.Show("Data=" + rt.data.context + "\r\n" + rt.data.location + "\r\n" + rt.data.time + "\r\n" + rt.data.ftime);
}
myStreamReader.Close(); //关闭
myStreamReader.Dispose(); //释放
myResponseStream.Close(); //关闭流
myResponseStream.Dispose(); //释放流
}
C#如何用Newtonsoft.Json获取[]中括号内多层[]中括号里的数据并显示在text里?
|