Get json formatted string from web by sending HttpWebRequest and then deserialize it to get needed data
static string GetLotteryByPhase(string phaseNo)
{
// Set the url and charset
string url = "http://baidu.lecai.com/lottery/draw/ajax_get_detail.php?lottery_type=50&phase=" + phaseNo;
string charset = "gb2312"; dynamic lotteryObj = null;
JArray redBalls = null;
JArray blueBall = null;
string lotteryStr = GetValueFromWeb(url, charset);
try
{
// Deserialize the json string
lotteryObj = JsonConvert.DeserializeObject(lotteryStr); // Get the expected data from json structure
redBalls = lotteryObj.data.result.result[].data;
blueBall = lotteryObj.data.result.result[].data;
}
catch (Exception)
{
return string.Empty;
} StringBuilder sb = new StringBuilder();
// Get the value from JToken by convrting the JArray to int array
int[] items = redBalls.Select(jv => (int)jv).ToArray(); foreach (int item in items)
{
sb.Append(item.ToString());
sb.Append(", ");
} // Get the value from JToken directly
string blueBallStr = ((int)blueBall[]).ToString();
sb.Append(blueBallStr); return sb.ToString(); ;
} // Retrieve data by sending http web request
public static string GetValueFromWeb(string Url, string CharSet)
{
string resultData = string.Empty; try
{
// Set HttpWebRequest's header
HttpWebRequest wReq = (HttpWebRequest)WebRequest.Create(Url);
wReq.UserAgent = "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";
wReq.ContentType = "application/x-www-form-urlencoded";
wReq.Accept = "*/*";
wReq.KeepAlive = true;
wReq.Headers.Add("Accept-Language", "zh-cn,en-us;q=0.5"); // Get and read the response
WebResponse wResp = wReq.GetResponse();
Stream respStream = wResp.GetResponseStream();
StreamReader reader = new StreamReader(respStream, Encoding.GetEncoding(CharSet)); resultData = reader.ReadToEnd(); return resultData;
}
catch
{
return resultData;
}
}
Get json formatted string from web by sending HttpWebRequest and then deserialize it to get needed data的更多相关文章
- JSON与String 格式的转换
JSON 可以用 .push() 添加新数据 var json =[ { title:"1",content:"Tom" }, { title:"2& ...
- JSON对象和字符串之间的相互转换JSON.stringify(obj)和JSON.parse(string)
在Firefox,chrome,opera,safari,ie9,ie8等高级浏览器直接可以用JSON对象的stringify()和parse()方法. JSON.stringify(obj)将JSO ...
- JSON 与 String、Map、JavaBean互转
JSON 与 String.Map.JavaBean互转 //解析远程登录用户信息 AttributePrincipal principal = AssertionHolder.getAssertio ...
- json和string 之间的相互转换
json和string 之间的相互转换 <script type="text/javascript"> //先认识一下js中json function showInfo ...
- json转String 和 String转json 和判断对象类型
function ajaxGetMenuList(){ $.getJSON("login.do", function(json){ var r = ""; zN ...
- 【JAVASCRIPT】json 与 string
1. 互相转换 JSON.stringify(jsonobj) json对象转化为string JSON.parse(string) string转化为json,注意{“name”:"hxy ...
- JSON与String之间互转
一,String转json 这个JSON.parse()与eval()都可以实现,但是它们是有区别的, JSON.parse对json字符串要求比eval()更为严格,key名称(例如name)全部必 ...
- JS中JSON和string字符串相互转换
在Firefox,chrome,opera,safari,ie9,ie8等高级浏览器直接可以用JSON对象的stringify()和parse()方法. JSON.stringify(obj)将JSO ...
- 小tips:JSON对象和字符串之间的相互转换JSON.stringify(obj)和JSON.parse(string)
在Firefox,chrome,opera,safari,ie9,ie8等高级浏览器直接可以用JSON对象的stringify()和parse()方法. JSON.stringify(obj)将JSO ...
随机推荐
- ES6简介
function fn(){ return 100; } let name='sui'; let age=19; let sui={ name, age, ["pro"+fn()] ...
- css中的层叠性及权重的比较
假如同一个标签被多个选择器选中,每个选择器都设置了相同的样式,浏览器中加载时这个样式听谁的? 不同选择器设置的同一个样式,只会选择一个进行加载,不会叠加. 为了解决听谁的问题,引入层叠性的概念. 层叠 ...
- multipath多路径实验02-配置多路径软件
multipath多路径实验02-配置多路径软件 在上一篇文章<multipath多路径实验01-构建iSCSI模拟环境>,我构建了iSCSI的模拟环境,在文章最后,已经成功配置并在主机上 ...
- C# 实现语音听写
本文系原创,禁止转载. 分享如何使用c#对接科大讯飞语音听写服务,简单高效地实现语音听写. 实现语音听写主要分为录音和语音识别两部分:录音是指获取设备声卡端口的音频数据并将之保存为音频文件,语音识别就 ...
- 配置ssh免密码登录——集群学习日记
度过了难熬的考试月时期之后,最近和小伙伴一起参加的的比赛进入了紧张的准备时期.在进行工作的时候,发现有很多基础的知识点,自己不是很清楚以及了解,所以在想,要不就边学习的时候边写下学习日记,以供自己后来 ...
- 配置mabatis,报Could not load driverClass ${jdbc.driverClassName}
<!-- 扫描mapper --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" ...
- Md5的加密 java实现
百度百科对MD5的说明是: Message Digest Algorithm MD5(中文名为消息摘要算法第 五版)为计算机安全领域广泛使用的一种散列函数,用以提供消息的完整性保护. MD5即Mess ...
- 定时任务FluentScheduler 学习笔记 .net
第一步添加引用 GitHub源码地址 与详细用法 https://github.com/fluentscheduler/FluentScheduler 下面开始简单的实现 /// <summar ...
- 关于戴尔没有活动分区,遇到了“Windows安装程序无法将windows配置为在此计算机的硬件上运行”提示等
虽然只有几句话,但是还是超级好用的 装过很多戴尔的笔记本,发现很多都是这里的问题导致系统装不进去. 1.硬盘设置有问题.进BIOS ,到SATA 设置看看硬盘是不是设置为AHCI了.这个ghost系统 ...
- 双向循环链表(C语言描述)(五)
代码清单 // dictionary.h #ifndef __DICTIONARY_H__ #define __DICTIONARY_H__ #include <assert.h> #in ...