https://blog.csdn.net/Sayesan/article/details/79756738

C# Newtonsoft.Json解析数组的小例子
 http://www.cnblogs.com/usharei/category/373071.html 有四篇很好的文章,建议学习下,但是现在的各种服务的json数据都是复杂类型的,本文只是补充一个复杂数据(数组)的小例子

上面提到的第四篇文章最后有个解析数组的例子,出现了
.First.First.First.First.Children();
我表示很晕,网上找的的例子大多数是关于JObject的,但是我很少看到JArray的例子,其实解析json数组的时候是需要用到JArray的,复杂数据实际上是JObject和JArray的组合:{}对应的是JObject,而[]对应的是JArray。举个json的例子吧(数据来源是腾讯地图api的示例,解析的是北京某处的地理信息和周边信息,略长啊)

[javascript] view plain copy
  1. {
  2. "status": 0,
  3. "message": "query ok",
  4. "result": {
  5. "address": "北京市海淀区彩和坊路海淀西大街74号",
  6. "address_component": {
  7. "province": "北京市",
  8. "city": "北京市",
  9. "district": "海淀区",
  10. "street": "彩和坊路",
  11. "street_number": "海淀西大街74号"
  12. },
  13. "pois": [
  14. {
  15. "id": "3629720141162880123",
  16. "title": "中国技术交易大厦",
  17. "address": "北京市海淀区北四环西路66号",
  18. "category": "房产小区;商务楼宇",
  19. "location": {
  20. "lat": "39.984122",
  21. "lng": "116.307484"
  22. },
  23. "_distance": "3.6"
  24. },
  25. {
  26. "id": "2845372667492951071",
  27. "title": "中国技术交易大厦A座",
  28. "address": "北京市海淀区北四环西路66号",
  29. "category": "房产小区;商务楼宇",
  30. "location": {
  31. "lat": "39.984273",
  32. "lng": "116.307577"
  33. },
  34. "_distance": "15.2"
  35. },
  36. {
  37. "id": "12925244666643621769",
  38. "title": "中国技术交易大厦B座",
  39. "address": "北京市海淀区北四环西路66号",
  40. "category": "房产小区;商务楼宇",
  41. "location": {
  42. "lat": "39.983902",
  43. "lng": "116.307588"
  44. },
  45. "_distance": "29.3"
  46. },
  47. {
  48. "id": "7472400256925846331",
  49. "title": "中国化工博物馆",
  50. "address": "海淀区北四环西路62号中国化工集团大厦3楼(海淀桥西)",
  51. "category": "文化场馆;博物馆",
  52. "location": {
  53. "lat": "39.984582",
  54. "lng": "116.308877"
  55. },
  56. "_distance": "127.5"
  57. },
  58. {
  59. "id": "16243165360295251323",
  60. "title": "贝塔咖啡",
  61. "address": "北京市海淀区北四环西路66号中关村第三极大厦1层西北侧",
  62. "category": "娱乐休闲;咖啡厅",
  63. "location": {
  64. "lat": "39.984391",
  65. "lng": "116.307380"
  66. },
  67. "_distance": "28.0"
  68. },
  69. {
  70. "id": "7246616758286733108",
  71. "title": "基督教堂",
  72. "address": "北京市海淀区彩和坊路9号",
  73. "category": "旅游景点;教堂",
  74. "location": {
  75. "lat": "39.983146",
  76. "lng": "116.307507"
  77. },
  78. "_distance": "112.2"
  79. },
  80. {
  81. "id": "8627298709465036679",
  82. "title": "北京三木和商店",
  83. "address": "北京市海淀区北四环西路66号中关村文化商厦三层D-006-009单元",
  84. "category": "购物;综合商场",
  85. "location": {
  86. "lat": "39.984093",
  87. "lng": "116.307983"
  88. },
  89. "_distance": "42.6"
  90. },
  91. {
  92. "id": "12020256857742021617",
  93. "title": "图书城昊海楼",
  94. "address": "北京市海淀区海淀西街36号",
  95. "category": "房产小区;住宅区;住宅小区",
  96. "location": {
  97. "lat": "39.984400",
  98. "lng": "116.306794"
  99. },
  100. "_distance": "65.4"
  101. },
  102. {
  103. "id": "10394251724976454044",
  104. "title": "北京点点酷东东商贸中心海淀分部",
  105. "address": "北京市海淀区北四环西路66号中关村文化商厦2B001",
  106. "category": "购物;综合商场",
  107. "location": {
  108. "lat": "39.984093",
  109. "lng": "116.307983"
  110. },
  111. "_distance": "42.6"
  112. },
  113. {
  114. "id": "16427755502147943355",
  115. "title": "北京资源燕园宾馆",
  116. "address": "北京市海淀区颐和园路1号",
  117. "category": "酒店宾馆;星级酒店",
  118. "location": {
  119. "lat": "39.986712",
  120. "lng": "116.305822"
  121. },
  122. "_distance": "318.3"
  123. }
  124. ]
  125. }
  126. }

我使用HttpRequest获取了这部分信息

[csharp] view plain copy
  1. HttpWebRequest request = (HttpWebRequest)result.AsyncState;
  2. HttpWebResponse response = (HttpWebResponse)(request.EndGetResponse(result));
  3. stream = response.GetResponseStream();
  4. StreamReader reader = new StreamReader(stream, false);
  5. string apiText = reader.ReadToEnd();
  6. JObject jsonObj = null;
  7. try
  8. {
  9. jsonObj = JObject.Parse(apiText);
  10. if (jsonObj.Count == 1 || (int)(jsonObj["status"]) != 0) this.isError = true;
  11. else
  12. {
  13. string provinceName = (string)jsonObj["result"]["address_component"]["province"];
  14. string cityName = this.cityName_s = (string)jsonObj["result"]["address_component"]["city"];
  15. string districtName = (string)jsonObj["result"]["address_component"]["district"];
  16. string street = (string)jsonObj["result"]["address_component"]["street"];
[csharp] view plain copy
  1. /*下面是解析JArray的部分*/
  2. JArray jlist = JArray.Parse(jsonObj["result"]["pois"].ToString()); //将pois部分视为一个JObject,JArray解析这个JObject的字符串
  3. LocationItem locationitem = null;      //存储附近的某个地点的信息
  4. locations = new List<LocationItem>();  //附近位置的列表
  5. for(int i = 0; i < jlist.Count ; ++i)  //遍历JArray
  6. {
  7. locationitem = new LocationItem();
  8. JObject tempo = JObject.Parse(jlist[i].ToString());
  9. locationitem.id = tempo["id"].ToString();
  10. locationitem.title = tempo["title"].ToString();
  11. locationitem._distance = tempo["_distance"].ToString();
  12. locationitem.address = tempo["address"].ToString();
  13. locationitem.category = tempo["category"].ToString();
  14. locationitem.location.lat = tempo["location"]["lat"].ToString();
  15. locationitem.location.lng = tempo["location"]["lng"].ToString();
  16. locations.Add(locationitem);
  17. }
  18. }
  19. }
  20. catch (Exception)
  21. {
  22. isError = true;
  23. }

其中使用了两个类

[csharp] view plain copy
  1. public class LngLat
  2. {
  3. public string lat { get; set; }
  4. public string lng { get; set; }
  5. }
  6. public class LocationItem
  7. {
  8. public string id{get;set;} //
  9. public string title { get; set; } //名称
  10. public string address { get; set; } //地址
  11. public string category { get; set; } //类型
  12. public LngLat location { get; set; } //经纬度
  13. public string _distance { get; set; } //距离(米)
  14. public LocationItem()
  15. {
  16. id = "0";
  17. title = "";
  18. address = "";
  19. _distance = "0";
  20. location = new LngLat { lng = "0", lat = "0" };
  21. category = "";
  22. }
  23. }

这样就完成了这个复杂json数据的解析。JSON数组访问还有用数组下标方式的,那个就需要数组至少要有足够的个数,如要取得上面那个json数据的
中国技术大厦A座 ,就是用 jsonObj["result"]["pois"][1]["title"].ToString()
,即访问了result下pois数组的第2个节点的title信息,但是要遍历所有的数据就明显不如JArray方便了

C# Newtonsoft.Json解析数组的小例子[转]的更多相关文章

  1. Newtonsoft.Json解析数组

    以下是解析json数组: var jsonInfo=[{"name":"abc","id":"1","coun ...

  2. [Cannot deserialize JSON array into type] NewtonSoft.Json解析数据出错原因

    今天用NewtonSoft.JSon解析一个天气数据,数据格式如: {"status":1,"detail":"\u6570\u636e\u83b7\ ...

  3. Newtonsoft.Json解析json字符串和写json字符串

    写: StringWriter sw = new StringWriter(); JsonWriter writer = new JsonWriter(sw); //如果报错则使用JsonWriter ...

  4. C# Newtonsoft.Json解析json字符串处理(最清晰易懂的方法)

    需求: 假设有如下json字符串: { ", "employees": [ { "firstName": "Bill", &quo ...

  5. c# 使用Newtonsoft.Json解析JSON数组

    一.获取JSon中某个项的值 要解析格式: [{"VBELN":"10","POSNR":"10","RET_ ...

  6. C# Newtonsoft.Json解析json字符串处理 - JToken 用法

    //*调用服务器API(获取可以处理的文件) //1.使用JSON通信协议(调用[待化验任务API]) String retData = null; { JToken json = JToken.Pa ...

  7. Newtonsoft.Json解析Json字符串案例:

    /// <summary> /// 上行jsom格式日志记录 /// </summary> /// <param name="responseJson" ...

  8. spring的一个小例子(二)--解析前面的小例子

    接上篇:http://www.cnblogs.com/xuejupo/p/5236448.html 首先应该明白,一个web项目,web.xml是入口. 然后下面来分析上篇博客中出现的web.xml: ...

  9. C# Newtonsoft.Json 解析多嵌套json 进行反序列化

    [ { ", "time": "2016-09-09 12:23:33", ", "freeShipping": tru ...

随机推荐

  1. Codeforces Round 542 (Div. 2)

    layout: post title: Codeforces Round 542 (Div. 2) author: "luowentaoaa" catalog: true tags ...

  2. 漏洞利用查询工具sandi

    漏洞利用查询工具sandi   在渗透测试中,一旦发现漏洞,就需要查找该漏洞的利用方式.由于漏洞众多,就需要渗透测试人员从海量的漏洞信息找出可用攻击载荷.Kali Linux内置了一个查询工具sand ...

  3. Outdated Kotlin Runtime

    你的 kotlin 运行时版本 在 1.1.2库中 是 1.1.2 然而插件版本是 1.1.4 . 运行时库 应该被更新,避免兼容问题. Outdated Kotlin Runtime Your ve ...

  4. qq sid qq sid 是什么 qq sid 怎么用

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha ======= qq sid qq sid  是什么 qq sid  怎么用 ===== ...

  5. MongoDB 那些坑(转)

    MongoDB 是目前炙手可热的 NoSQL 文档型数据库,它提供的一些特性很棒:如自动 failover 机制,自动 sharding,无模式 schemaless,大部分情况下性能也很棒.但是薄荷 ...

  6. 关于STM32 SPI NSS的讨论

    NSS分为内部引脚和外部引脚. NSS外部引脚可以作为输入信号或者输出信号, 输入信号一般用作硬件方式从机的片选, 而输出信号一般用于主SPI去片选与之相连的从SPI. NSS从设备选择有两种模式: ...

  7. [Go] 第一个单词首字母变大写:Ucfirst(),第一个单词首字母变小写:Lcfirst()

    import ( "unicode" ) func Ucfirst(str string) string { for i, v := range str { return stri ...

  8. jquery智能弹出层,自己主动推断位置

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. 快速打开 Mac OS X 隐藏的用户资源库文件夹

    在较高版本的 Mac OS X 中,用户的资源库文件夹(/Users/username/Library)默认被系统隐藏了,从 Finder 窗口中不能直接打开. 下面介绍一个非常简单的方法来快速打开用 ...

  10. 恶意软件正在利用SSLserver窃取用户个人信息!

    安全套接层协议(SSL)及安全传输层协议(TLS)旨在提供一个安全.加密的client和server之间的连接网络.为进一步进行身份验证和加密,server必须提供证书,从而直接有效地证明其身份. 使 ...