当前位置:网站首页>C#从网址异步获得json格式的数据
C#从网址异步获得json格式的数据
2022-07-29 02:43:00 【孔雀东南飞-深圳】
1 开始访问
public static void StartAccessingURL(string url,int index)
{
try
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Method = "Get";
webRequest.ContentType = "application/json";
webRequest.Headers.Add("Authorization", "MzQ1"); //token
webRequest.BeginGetResponse(new AsyncCallback(OnOneResponse), webRequest);
}
catch(Exception ex)
{
Log.LogHelper.Error("StartAccessingURL() " + ex.Message);
}
}2 回调函数
public static void OnOneResponse(IAsyncResult ar)
{
try
{
WebRequest wrq = (WebRequest)ar.AsyncState;
WebResponse wrs = wrq.EndGetResponse(ar);
HttpWebResponse httpWebResponse = (HttpWebResponse)wrs;
StreamReader reader = new StreamReader(httpWebResponse.GetResponseStream(), Encoding.UTF8);
string data = reader.ReadToEnd();
reader.Close();
httpWebResponse.Close();
getData(data);
}
catch(Exception e)
{
Log.LogHelper.Error("OnOneResponse" + e.Message);
}
}3 获得数据
public static void getData(string str)
{
try
{
OrderItem oi = new OrderItem();
JObject ja = (JObject)JsonConvert.DeserializeObject(str);
string json = ja.Last.ToString();
setDoubleToSingle(ref json);
var resultObjects = AllChildren(JObject.Parse(json)).
First(c => c.Type == JTokenType.Array && c.Path.Contains("data")).
Children<JObject>();
foreach (JObject result in resultObjects)
{
oi = JsonConvert.DeserializeObject<OrderItem>(result.ToString());
observation.Add(oi);
}
}
catch (Exception e)
{
Log.LogHelper.Error("getData() " + e.Message);
}
}4 其他
#region 遍历
private static IEnumerable<JToken> AllChildren(JToken json)
{
foreach (var c in json.Children())
{
yield return c;
foreach (var cc in AllChildren(c))
{
yield return cc;
}
}
}
#endregion
#region 将双引号变成单引号
public static void setDoubleToSingle(ref string str)
{
str = str.Replace("\r\n", "");
str = str.Replace("\"", "'");
str = str.Insert(0, "{");
str = str.Insert(str.Length, "}");
}
#endregion
边栏推荐
- [error reporting] node:internal/modules/cjs/loader:936 [solution]
- qt QStringList用法
- Family relationship calculator wechat applet source code
- 向DataFrame中的特定位置添加一行
- MySQL 操作数据库数据报错:Fatal error encountered during command execution
- Redis queue realizes second kill
- Etcd implementation of large-scale service governance application practice
- 自组织是管理者和成员的双向奔赴
- 【luogu P8352】小 N 的独立集(DP套DP)(性质)
- owt-server源码剖析(三)--video模块分析之Mixer In
猜你喜欢

冰冰学习笔记:运算符重载---日期类的实现

TP5.0 小程序用户无需登录,直接获取用户手机号。

Summary of classic problems in Flink production environment

SOA(面向服务架构)是什么?
![[opencv] use OpenCV to call mobile camera](/img/66/6207bafbc9696e43da7a60a386a238.jpg)
[opencv] use OpenCV to call mobile camera

php 进程通信系列 (一) 命名管道

Flink生产环境经典问题汇总

Etcd implementation of large-scale service governance application practice

并发模式之异步回调Future模式

Stm32c8t6 encoder motor speed measurement and Arduino photoelectric module speed measurement
随机推荐
架构师进阶,微服务设计与治理的 16 条常用原则
MySQL compound query (important)
Mysql复合查询(重要)
SOA(面向服务架构)是什么?
[QNX Hypervisor 2.2用户手册]9.11 ram(更新中)
UE4.25 Slate源码解读
Redis队列实现秒杀
Cloud development pocket toolbox wechat applet source code
.NET 序列化枚举为字符串
ECCV 2022 | airdet: a small sample target detection method without fine tuning
OWT server source code analysis (4) -- video module analysis of mixer out
数据截断及估计
Advanced architects, 16 common principles of microservice design and Governance
centos安装mysql8
【OpenCV】使用OpenCV调用手机摄像头
C language: hollow square pattern
DHCP协议详细解析
Available data sets for scene classification tasks (part)
【npm错误】- npm ERR code ERESOLVE 和 npm ERR ERESOLVE could not resolve 问题
R语言ERROR: compilation failed for package ‘****‘