c#序列化json字符串及处理
上面提到的第四篇文章最后有个解析数组的例子,出现了 .First.First.First.First.Children(); 我表示很晕,网上找的的例子大多数是关于JObject的,但是我很少看到JArray的例子,其实解析json数组的时候是需要用到JArray的,复杂数据实际上是JObject和JArray的组合:{}对应的是JObject,而[]对应的是JArray。举个json的例子吧(数据来源是腾讯地图api的示例,解析的是北京某处的地理信息和周边信息,略长啊)
引用: Newtonsoft.Json
- {
- "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#序列化json字符串及处理的更多相关文章
- 此类目的是防治序列化Json字符串时的循环引用问题-------最好解决方案
http://james.newtonking.com/json/help/index.html using Newtonsoft.Json;using System;using System.Col ...
- C# 把对象序列化 JSON 字符串 和把JSON字符串还原为对象
/// <summary> /// 把对象序列化 JSON 字符串 /// </summary> /// <typeparam name="T"> ...
- 表单序列化json字符串和js时间格式化
js时间格式化 new Date().format("时间格式") Date.prototype.format = function(fmt) { var o = { ...
- c# 使用 Newtonsoft.Json 序列化json字符串以及,反序列化对象
1. 序列化 对象 /** 使用 Newtonsoft.Json 序列化对象 **/ [WebMethod] public String getPersonInfos() { // 初始化数据 Lis ...
- [MVC_Json序列化]Json字符串反序列化成C#对象
上一篇中有Json序列化相关问题得到了解决. 那么结果集为Json串时,如何将Json串转成C#对象呢? 现举例说明: -现有如下字符串数据 string k = "{\"ring ...
- js将form表单序列化[json字符串、数组、对象]
1.序列化为字符串 $("#Form").serialize();//name=zhangsan&sex=1&age=20 2.序列化为数组 var formD ...
- ASP.NET自带对象JSON字符串与实体类的转换
关于JSON的更多介绍,请各位自行google了解!如果要我写的话,我也是去Google后copy!嘿嘿,一直以来很想学习json,大量的找资料和写demo,总算有点了解! 切入正题! 还是先封装一个 ...
- Newtonsoft.Json.dll 反序列化JSON字符串
上一篇JSON博客<JSON入门级学习小结--JSON数据结构>中已对JSON做了简单介绍,JSON字符串数组数据样式大概是这样子的: 如今因为项目需求(asp.net web网站,前台向 ...
- C#将JSON字符串对象序列化与反序列化
C#将对象序列化成JSON字符串 public string GetJsonString() { List<Product> products = new List<Product& ...
随机推荐
- [机器学习] ——KNN K-最邻近算法
KNN分类算法,是理论上比较成熟的方法,也是最简单的机器学习算法之一. 该方法的思路是:如果一个样本在特征空间中的k个最相似(即特征空间中最邻近)的样本中的大多数属于某一个类别,则该样本也属于这个类别 ...
- PHP TCPDF ERROR: [Image] Unable to get image解决办法详解
使用TCPDF输出pdf文件时,有时会直接显示pdf文件不可显示,仔细调试之下会报错TCPDF ERROR: [Image] Unable to get image.问题出现Image()函数中.第一 ...
- CentOS7 编译安装 Mariadb (实测 笔记 Centos 7.0 + Mariadb 10.0.15)
环境: 系统硬件:vmware vsphere (CPU:2*4核,内存2G,双网卡) 系统版本:CentOS-7.0-1406-x86_64-DVD.iso 安装步骤: 1.准备 1.1 显示系统版 ...
- javascript学习之时间组件
写了一个时间组件,哪里需要哪里调(菜鸟级别,大牛路过就Ok了): 先有一个HTML文件: <!doctype> <html> <head> <title> ...
- Java WebService 简单实例
前言:朋友们开始以下教程前,请先看第五大点的注意事项,以避免不必要的重复操作. 一.准备工作(以下为本实例使用工具) 1.MyEclipse10.7.1 2.JDK 1.6.0_22 二.创建服务端 ...
- 使用XML文件记录操作日志,并从后往前读取操作日志并在richTextBox1控件中显示出来
#region 获取本地程序操作记录日志 /// <summary> /// 获取本地程序更新日志信息(由后往前读取) /// </summary> private void ...
- CentOS下设置默认JDK
最近在弄Linux,用yum源安装opnjdk-devel版本后,用命令ll /etc/alternatives/java查看,发现指向的是jre目录,而不是jdk,在此设置指向jdk目录. 1. 设 ...
- 3.线性表-cursor
fatal.h #include <stdio.h> #include <stdlib.h> #define Error( Str ) FatalError( Str ) #d ...
- Linux防火墙配置(iptables, firewalld)
netfilter和底层实现 iptables firealld Linux中的防火墙 RHEL中有几种防火墙共存: iptables firewalld ip6tables ebtables 这些软 ...
- 使用 Composer 为 ThinkPHP(3.2.3)框架添加和管理组件
环境:Windows 64位 PHP 版本: 框架:ThinkPHP Tips: 组件:打包的代码,可以是一系列相关的类(class).接口(interface).特性(trait),用于解决某个具体 ...