在百度 API Store 找个旅游的 API 来当成本次 Demo 的例子

接口地址:http://apis.baidu.com/apistore/attractions/spot
AIPKEY: ba491eeaaedf24f4fb20f59e9df27eb6

先拿到返回的 Json 字符串:

 /// <summary>
/// 发送HTTP请求
/// </summary>
/// <param name="url">请求的URL</param>
/// <param name="param">请求的参数</param>
/// <returns>请求结果</returns>
public static string request(string url, string param)
{
string strURL = url + '?' + param;
System.Net.HttpWebRequest request;
request = (System.Net.HttpWebRequest)WebRequest.Create(strURL);
request.Method = "GET";
// 添加header
request.Headers.Add("apikey", "ba491eeaaedf24f4fb20f59e9df27eb6");
System.Net.HttpWebResponse response;
response = (System.Net.HttpWebResponse)request.GetResponse();
System.IO.Stream s;
s = response.GetResponseStream();
string StrDate = "";
string strValue = "";
StreamReader Reader = new StreamReader(s, Encoding.UTF8);
while ((StrDate = Reader.ReadLine()) != null)
{
strValue += StrDate + "\r\n";
}
return strValue;
}

调用:

 string url = "http://apis.baidu.com/apistore/attractions/spot";
string param = "id=yiheyuan&output=json";
string jsonText = request(url, param);

使用 Newtonsoft.Json.dll 来进行 Json 的序列化 :

string json = JsonConvert.DeserializeObject(jsonText).ToString();

得到的 Json 字符串:

{
"error": 0,
"status": "Success",
"date": "2015-09-09",
"result": {
"name": "颐和园",
"location": {
"lng": 116.26844967549,
"lat": 39.992182483805
},
"telephone": "010-62881144",
"star": "5",
"url": "http://lvyou.baidu.com/yiheyuan",
"abstract": "我国现存规模最大、保存最完整的皇家园林,久负盛名。",
"description": "颐和园位于北京西北郊海淀区内,距北京城区15公里,是我国现存规模最大,保存最完整的皇家园林之一,也是享誉世界的旅游胜地之一。 颐和园是利用昆明湖、万寿山为基址,以杭州西湖风景为蓝本,汲取江南园林的某些设计手法和意境而建成的一座大型天然山水园,也是保存得最完整的一座皇家行宫御苑,被誉为皇家园林博物馆。 颐和园景区规模宏大,园内建筑以佛香阁为中心,园中有景点建筑物百余座、大小院落20余处,3555古建筑,面积70000多平方米,共有亭、台、楼、阁、廊、榭等不同形式的建筑3000多间。古树名木1600余株。其中佛香阁、长廊、石舫、苏州街、十七孔桥、谐趣园、大戏台等都已成为家喻户晓的代表性建筑。",
"ticket_info": {
"price": "1. 旺季(4月1日~10月31日):30.00元 德和园:5.00元 佛香阁:10.00元 苏州街:10.00元 文昌院:20.00元 联票(含门票、文昌院、德和园、佛香阁、苏州街澹宁堂):60.00元 2. 淡季(11月1日~3月31日):20.00元 德和园:5.00元 佛香阁:10.00元 苏州街:10.00元 文昌院:20.00元 联票(含门票、文昌院、德和园、佛香阁、苏州街澹宁堂):50.00元",
"open_time": "1. 旺季(4月1日~10月31日):06:30~20:00 停止售票时间:18:00 园中园(含文昌院、德和园、佛香阁、苏州街澹宁堂):08:30~17:00 2. 淡季(11月1日~3月31日):07:00~19:00 停止售票时间:17:00 园中园(含文昌院、德和园、佛香阁、苏州街澹宁堂):09:00~18:00",
"attention": [
{
"name": "【门票优惠政策】",
"description": "1. 身高1.2米以下儿童免票。 2. 北京市65岁以上老年人凭老年证免票;外地70周岁以上(含70周岁)老年人凭有效证件,门票半价优惠。 3. 大、中、小学学生(不含成人教育学生)、外国留学生凭学生证,门票半价优惠。 4. 残疾人、离休干部、离休军人、现役军人、武警官兵、省、部级以上劳模凭有效证件免票。 5. 持有社会保障金领取证的人员凭有效证件,门票半价优惠。"
}
]
}
}
}

解析这个 Json 并且拿到以上 key 对应的 value 代码如下:

先根据拿到的 Json 字符串中的 key 对象属性来定义 Model 类:

public class Location
{
public double lng { get; set; }
public double lat { get; set; }
} public class Attention
{
public string name { get; set; }
public string description { get; set; }
} public class TicketInfo
{
public string price { get; set; }
public string open_time { get; set; }
public List<Attention> attention { get; set; }
} public class Result
{
public string name { get; set; }
public Location location { get; set; }
public string telephone { get; set; }
public string star { get; set; }
public string url { get; set; }
public string @abstract { get; set; }
public string description { get; set; }
public TicketInfo ticket_info { get; set; }
} public class RootObject
{
public int error { get; set; }
public string status { get; set; }
public string date { get; set; }
public Result result { get; set; }
}

获取所有的 key & value:

 var rs = JsonConvert.DeserializeObject<RootObject>(json);

NewtonSoft.json 序列化和反序列化实例的更多相关文章

  1. Newtonsoft.Json 序列化和反序列化 以及时间格式 2 高级使用

    手机端应用讲究速度快,体验好.刚好手头上的一个项目服务端接口有性能问题,需要进行优化.在接口多次修改中,实体添加了很多字段用于中间计算或者存储,然后最终用Newtonsoft.Json进行序列化返回数 ...

  2. Newtonsoft.Json 序列化和反序列化 时间格式

    From : http://www.cnblogs.com/litian/p/3870975.html 1.JSON序列化 string JsonStr= JsonConvert.SerializeO ...

  3. Newtonsoft.Json 序列化和反序列化 时间格式 [转]

    1.JSON序列化 string JsonStr= JsonConvert.SerializeObject(Entity); eg:   A a=new A(); a.Name="Elain ...

  4. [转]Newtonsoft.Json 序列化和反序列化 时间格式

    本文转自:http://www.cnblogs.com/litian/p/3870975.html 1.JSON序列化 string JsonStr= JsonConvert.SerializeObj ...

  5. Newtonsoft.Json 序列化和反序列化 以及时间格式

    1.JSON序列化 string JsonStr= JsonConvert.SerializeObject(Entity); eg:   A a=new A(); a.Name="Elain ...

  6. Newtonsoft.Json 序列化和反序列化 时间格式【转】

    1.JSON序列化 string JsonStr= JsonConvert.SerializeObject(Entity); eg:   A a=new A(); a.Name="Elain ...

  7. newtonsoft.json 序列化,反序列化

    public class Book { public string BookID { get; set; } public DateTime PublishDate { get; set; } pub ...

  8. Newtonsoft.Json 序列化和反序列化 以及时间格式 2

    一.JSON使用JsonPropertyAttribute重命名属性名 1.先创建一个Movie对象,然后在其属性上添加JsonProperty,并指定重命名的名称.注意:属性Name和Directo ...

  9. 使用Newtonsoft.Json序列化和反序列化对象(源码)

    Json数据格式,简单而强大. 使用Json,不得不提到Newtonsoft.Json,它帮助我们更方便的使用Json,当然,不使用它也是可以的,还有许多方法将对象序列化成Json字符串,暂且不提. ...

随机推荐

  1. 超大型 LED 显示屏

    http://acm.hunnu.edu.cn/online/?action=problem&type=show&id=11574&courseid=0 题目 E. 超大型 L ...

  2. Linux使用sudo提权时,出现xx 不在 sudoers 文件中。此事将被报告。visudo 命令简单介绍。

    在使用 sudo 临时提权时,出现:不在 sudoers 文件中.此事将被报告. 可以使用 visudo命令 来配置/etc/sudoers文件,将目标用户赋予使用sudo命令的能力. visudo命 ...

  3. chm 字体修改

    今天打开从网络下载的“[MSDN]Csharp编程指南+参考手册.chm”文件,以为看看里面所提供的一些知识点,但是发现文件显示的字体觉得有点别扭,以为能够像网页那样ctrl键+鼠标滚轮就能进行字体的 ...

  4. 【HTML】Beginner6:Link

    1.Link HTML wich basically means a system of linked text     link to another HTML file or any file a ...

  5. 韦东山yy公开课笔记(2)--汇编,段,栈,重定位/链接地址,位置无关吗

    1. 要不要学习汇编 可以只懂一点,工作中基本不用,一旦用就是出了大问题 ldr : load 读内存 ldr r0, [r1]  : r1里存放的是地址值, 去这个地址读取4字节的内容,存入r0 s ...

  6. Visual Studio的.NET内存分配分析器解析

    Visual Studio 2012拥有丰富的有价值的功能,以至于我听到开发者反馈的需要的新功能新版本已经有了.另外,我听到开发人员询问具体的功能的某个特性,实际上他真正需要的是另外一个功能点. 上面 ...

  7. localtime和localtime_r

    上程序: #include <cstdlib> #include <iostream> #include <time.h> #include <stdio.h ...

  8. UVALive 5111 Soccer Teams (动态规划)

    题意:给指定数量的数字“1”,“2”,“3”……,“9”.用所有这些数字加上任意个0组成一个数,要求数能被11整除,且数的位数尽量小. 能被11整除的数有一个特点,奇数位数字之和与偶数位之和的差为11 ...

  9. hdoj 1702 ACboy needs your help again!【数组模拟+STL实现】

    ACboy needs your help again! Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  10. jQuery EasyUI, datagrid, treegrid formatter 参数比较 row index

    如题: datagrid中,见官方文档: formatter function The cell formatter function, take three parameter:value: the ...