C# Newtonsoft.Json解析数组的小例子[转]
https://blog.csdn.net/Sayesan/article/details/79756738
上面提到的第四篇文章最后有个解析数组的例子,出现了
.First.First.First.First.Children();
我表示很晕,网上找的的例子大多数是关于JObject的,但是我很少看到JArray的例子,其实解析json数组的时候是需要用到JArray的,复杂数据实际上是JObject和JArray的组合:{}对应的是JObject,而[]对应的是JArray。举个json的例子吧(数据来源是腾讯地图api的示例,解析的是北京某处的地理信息和周边信息,略长啊)
- {
- "status": 0,
- "message": "query ok",
- "result": {
- "address": "北京市海淀区彩和坊路海淀西大街74号",
- "address_component": {
- "province": "北京市",
- "city": "北京市",
- "district": "海淀区",
- "street": "彩和坊路",
- "street_number": "海淀西大街74号"
- },
- "pois": [
- {
- "id": "3629720141162880123",
- "title": "中国技术交易大厦",
- "address": "北京市海淀区北四环西路66号",
- "category": "房产小区;商务楼宇",
- "location": {
- "lat": "39.984122",
- "lng": "116.307484"
- },
- "_distance": "3.6"
- },
- {
- "id": "2845372667492951071",
- "title": "中国技术交易大厦A座",
- "address": "北京市海淀区北四环西路66号",
- "category": "房产小区;商务楼宇",
- "location": {
- "lat": "39.984273",
- "lng": "116.307577"
- },
- "_distance": "15.2"
- },
- {
- "id": "12925244666643621769",
- "title": "中国技术交易大厦B座",
- "address": "北京市海淀区北四环西路66号",
- "category": "房产小区;商务楼宇",
- "location": {
- "lat": "39.983902",
- "lng": "116.307588"
- },
- "_distance": "29.3"
- },
- {
- "id": "7472400256925846331",
- "title": "中国化工博物馆",
- "address": "海淀区北四环西路62号中国化工集团大厦3楼(海淀桥西)",
- "category": "文化场馆;博物馆",
- "location": {
- "lat": "39.984582",
- "lng": "116.308877"
- },
- "_distance": "127.5"
- },
- {
- "id": "16243165360295251323",
- "title": "贝塔咖啡",
- "address": "北京市海淀区北四环西路66号中关村第三极大厦1层西北侧",
- "category": "娱乐休闲;咖啡厅",
- "location": {
- "lat": "39.984391",
- "lng": "116.307380"
- },
- "_distance": "28.0"
- },
- {
- "id": "7246616758286733108",
- "title": "基督教堂",
- "address": "北京市海淀区彩和坊路9号",
- "category": "旅游景点;教堂",
- "location": {
- "lat": "39.983146",
- "lng": "116.307507"
- },
- "_distance": "112.2"
- },
- {
- "id": "8627298709465036679",
- "title": "北京三木和商店",
- "address": "北京市海淀区北四环西路66号中关村文化商厦三层D-006-009单元",
- "category": "购物;综合商场",
- "location": {
- "lat": "39.984093",
- "lng": "116.307983"
- },
- "_distance": "42.6"
- },
- {
- "id": "12020256857742021617",
- "title": "图书城昊海楼",
- "address": "北京市海淀区海淀西街36号",
- "category": "房产小区;住宅区;住宅小区",
- "location": {
- "lat": "39.984400",
- "lng": "116.306794"
- },
- "_distance": "65.4"
- },
- {
- "id": "10394251724976454044",
- "title": "北京点点酷东东商贸中心海淀分部",
- "address": "北京市海淀区北四环西路66号中关村文化商厦2B001",
- "category": "购物;综合商场",
- "location": {
- "lat": "39.984093",
- "lng": "116.307983"
- },
- "_distance": "42.6"
- },
- {
- "id": "16427755502147943355",
- "title": "北京资源燕园宾馆",
- "address": "北京市海淀区颐和园路1号",
- "category": "酒店宾馆;星级酒店",
- "location": {
- "lat": "39.986712",
- "lng": "116.305822"
- },
- "_distance": "318.3"
- }
- ]
- }
- }
我使用HttpRequest获取了这部分信息
- HttpWebRequest request = (HttpWebRequest)result.AsyncState;
- HttpWebResponse response = (HttpWebResponse)(request.EndGetResponse(result));
- stream = response.GetResponseStream();
- StreamReader reader = new StreamReader(stream, false);
- string apiText = reader.ReadToEnd();
- JObject jsonObj = null;
- try
- {
- jsonObj = JObject.Parse(apiText);
- if (jsonObj.Count == 1 || (int)(jsonObj["status"]) != 0) this.isError = true;
- else
- {
- string provinceName = (string)jsonObj["result"]["address_component"]["province"];
- string cityName = this.cityName_s = (string)jsonObj["result"]["address_component"]["city"];
- string districtName = (string)jsonObj["result"]["address_component"]["district"];
- string street = (string)jsonObj["result"]["address_component"]["street"];
- /*下面是解析JArray的部分*/
- JArray jlist = JArray.Parse(jsonObj["result"]["pois"].ToString()); //将pois部分视为一个JObject,JArray解析这个JObject的字符串
- LocationItem locationitem = null; //存储附近的某个地点的信息
- locations = new List<LocationItem>(); //附近位置的列表
- for(int i = 0; i < jlist.Count ; ++i) //遍历JArray
- {
- locationitem = new LocationItem();
- JObject tempo = JObject.Parse(jlist[i].ToString());
- locationitem.id = tempo["id"].ToString();
- locationitem.title = tempo["title"].ToString();
- locationitem._distance = tempo["_distance"].ToString();
- locationitem.address = tempo["address"].ToString();
- locationitem.category = tempo["category"].ToString();
- locationitem.location.lat = tempo["location"]["lat"].ToString();
- locationitem.location.lng = tempo["location"]["lng"].ToString();
- locations.Add(locationitem);
- }
- }
- }
- catch (Exception)
- {
- isError = true;
- }
其中使用了两个类
- public class LngLat
- {
- public string lat { get; set; }
- public string lng { get; set; }
- }
- public class LocationItem
- {
- public string id{get;set;} //
- public string title { get; set; } //名称
- public string address { get; set; } //地址
- public string category { get; set; } //类型
- public LngLat location { get; set; } //经纬度
- public string _distance { get; set; } //距离(米)
- public LocationItem()
- {
- id = "0";
- title = "";
- address = "";
- _distance = "0";
- location = new LngLat { lng = "0", lat = "0" };
- category = "";
- }
- }
这样就完成了这个复杂json数据的解析。JSON数组访问还有用数组下标方式的,那个就需要数组至少要有足够的个数,如要取得上面那个json数据的
中国技术大厦A座 ,就是用 jsonObj["result"]["pois"][1]["title"].ToString()
,即访问了result下pois数组的第2个节点的title信息,但是要遍历所有的数据就明显不如JArray方便了
C# Newtonsoft.Json解析数组的小例子[转]的更多相关文章
- Newtonsoft.Json解析数组
以下是解析json数组: var jsonInfo=[{"name":"abc","id":"1","coun ...
- [Cannot deserialize JSON array into type] NewtonSoft.Json解析数据出错原因
今天用NewtonSoft.JSon解析一个天气数据,数据格式如: {"status":1,"detail":"\u6570\u636e\u83b7\ ...
- Newtonsoft.Json解析json字符串和写json字符串
写: StringWriter sw = new StringWriter(); JsonWriter writer = new JsonWriter(sw); //如果报错则使用JsonWriter ...
- C# Newtonsoft.Json解析json字符串处理(最清晰易懂的方法)
需求: 假设有如下json字符串: { ", "employees": [ { "firstName": "Bill", &quo ...
- c# 使用Newtonsoft.Json解析JSON数组
一.获取JSon中某个项的值 要解析格式: [{"VBELN":"10","POSNR":"10","RET_ ...
- C# Newtonsoft.Json解析json字符串处理 - JToken 用法
//*调用服务器API(获取可以处理的文件) //1.使用JSON通信协议(调用[待化验任务API]) String retData = null; { JToken json = JToken.Pa ...
- Newtonsoft.Json解析Json字符串案例:
/// <summary> /// 上行jsom格式日志记录 /// </summary> /// <param name="responseJson" ...
- spring的一个小例子(二)--解析前面的小例子
接上篇:http://www.cnblogs.com/xuejupo/p/5236448.html 首先应该明白,一个web项目,web.xml是入口. 然后下面来分析上篇博客中出现的web.xml: ...
- C# Newtonsoft.Json 解析多嵌套json 进行反序列化
[ { ", "time": "2016-09-09 12:23:33", ", "freeShipping": tru ...
随机推荐
- 用yum安装JDK
用yum安装JDK 1.查看yum库中都有哪些jdk版本(暂时只发现了openjdk) [root@localhost ~]# yum search java|grep jdkldapjdk-java ...
- jquery获取系统当前时间
//判断是否在前面加0function getNow(s) { return s < 10 ? '0' + s: s;} var myDate = new Date(); var year=my ...
- listview重新计算高度
将xml中的ListView改用下面的ListViewForScrollView //ScrollView中嵌入ListView,让ListView全显示出来 public class ListVie ...
- Linux下Nginx的监控
一.安装Nginx 使用源码编译安装,包括具体的编译参数信息. 正式开始前,编译环境gcc g++ 开发库之类的需要提前装好. 安装make: yum -y install gcc automake ...
- Codeforces 235E. Number Challenge DP
dp(a,b,c,p) = sigma ( dp(a/p^i,b/p^j,c/p^k) * ( 1+i+j+k) ) 表示用小于等于p的素数去分解的结果有多少个 E. Number Challenge ...
- Go Session 使用简介
6.session和数据存储 6.1 session和cookie 6.2 Go如何使用session 6.3 session存储 6.4 预防session劫持 6.5 小结
- linux虚拟机与winodows共享文件夹----linux安装VMware tools
虚拟机里面想要获取原来本机 系统的文件,十分麻烦.为了实现原系统与虚拟机的共享文件夹,可以通过安装vmware tools达到共享目的. 1 安装vmware tools (1)检查虚拟机上是否挂 ...
- 交叉编译Python-3.6.0到aarch64/aarch32 —— 支持sqlite3
参考 https://datko.net/2013/05/10/cross-compiling-python-3-3-1-for-beaglebone-arm-angstrom/ 平台 主机: ubu ...
- 在ASP.NET MVC中实现Select多选
我们知道,在ASP.NET MVC中实现多选Select的话,使用Html.ListBoxFor或Html.ListBox方法就可以.在实际应用中,到底该如何设计View Model, 控制器如何接收 ...
- 写一个限制上传文件大小和格式的jQuery插件
在客户端上传文件,通常需要限制文件的尺寸和格式,最常用的做法是使用某款插件,一些成熟的插件的确界面好看,且功能强大,但美中不足的是:有时候会碰到浏览器兼容问题.本篇就来写一个"原生态&quo ...