本帖最后由 wfr666 于 2023-5-26 23:23 编辑
[C#] 纯文本查看 复制代码 Class1类
public class Root1
{
public List<List<object>> n { get; set; } //已转换的Json实体类
}
引用:
using System.IO;
using System.Net;
using Newtonsoft.Json;
private void button2_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); //流读取器,观察者模式
string getJson = myStreamReader.ReadToEnd();
listView2.Items.Clear();
Root1 rt1 = JsonConvert.DeserializeObject<Root1>(getJson);
StringBuilder sb1 = new StringBuilder();
foreach (object item in rt1.n) //遍历第一层?
{
if (item is List<object> innerList)
{
foreach (object innerItem in innerList) //遍历第二层?
{
if (innerItem is List<object> nestedList)
{
object thirdValue = nestedList[0]; //获取指定的值,为何提示"没有"?
sb1.Append(thirdValue.ToString());
}
else
{
sb1.Append("没有");
}
sb1.Append(Environment.NewLine);
}
}
}
textBox1.Text = sb1.ToString();
myStreamReader.Close(); //关闭
myStreamReader.Dispose(); //释放
myResponseStream.Close(); //关闭流
myResponseStream.Dispose(); //释放流
}
}
}
https://phoile.tender88.com/spor ... 147061&locale=zh_CN
以上链接是网页接口的json数据,太多嵌套了.
如图:
遍历索引列表里,再遍历里面的列表, 一层一层的遍历, 假如我要获取图右边里的值"3.200",该如何实现?
|