C#处理JSON 数据
网络中数据传输经常是xml或者json,现在做的一个项目之前调其他系统接口都是返回的xml格式,刚刚遇到一个返回json格式数据的接口,通过例子由易到难总结一下处理过程,希望能帮到和我一样开始不会的朋友。
一、C#处理简单json数据
json数据:
{"result":"","res_info":"ok","queryorder_info":"info"}
我这里是以jsonmy1.txt文件的形式保存在d盘json文件夹下。
构造对象:
public struct ToJsonMy
{
public string result { get; set; } //属性的名字,必须与json格式字符串中的"key"值一样。
public string res_info { get; set; }
public string queryorder_info { get; set; }
}
public static void JsonMy()
{
string json = Jsonstr("D:\\json\\jsonmy1.txt");//Jsonstr函数读取json数据的文本txt
JavaScriptSerializer js = new JavaScriptSerializer(); //实例化一个能够序列化数据的类
ToJsonMy list = js.Deserialize<ToJsonMy>(json); //将json数据转化为对象类型并赋值给list
string result = list.result;
string res_info = list.res_info;
string queryorder_info = list.res_info;
}
二、C#处理包含对象的json数据
json数据:jsonmy2.txt
{"result":"","res_info":"ok","queryorder_info":{"order_num":"","orderdetail":"ok"}}
构造对象:
public struct ToJsonMy2
{
public string result { get; set; }
public string res_info { get; set; }
public queryorder_info queryorder_info;
}
public struct queryorder_info
{
public string order_num { get; set; }
public string orderdetail { get; set; }
};
转换过程:
public static void JsonMy2()
{
string json = Jsonstr("D:\\json\\jsonmy2.txt");
JavaScriptSerializer js = new JavaScriptSerializer(); //实例化一个能够序列化数据的类
ToJsonMy2 list = js.Deserialize<ToJsonMy2>(json); //将json数据转化为对象类型并赋值给list
string result = list.result;
string res_info = list.res_info;
string order_num = list.queryorder_info.order_num;
string orderdetail = list.queryorder_info.orderdetail;
}
三、C#处理包含对象、数组的json数据
json数据:jsonmy4.txt
{"result":"","res_info":"ok","queryorder_info":{"order_num":"","orderdetail":[{"CFTUin":"","CancelDeadline":"2013-09-12 23:00:00","CheckInDate":"2013-09-12 00:00:00","CheckOutDate":"2013-09-13 00:00:00","CityID":"","CurrencyCode":"RMB","HotelID":"","HotelName":"乐家连锁(北京天坛南门店)(原速8酒店(北京天坛南门店)","ListID":"","PayAmt":"","PayType":"","RommsCnt":"","SPTransID":"","State":""},{"CFTUin":"","CancelDeadline":"2013-10-13 23:00:00","CheckInDate":"2013-10-13 00:00:00","CheckOutDate":"2013-10-18 00:00:00","CityID":"","CurrencyCode":"RMB","HotelID":"","HotelName":"上海凯顿酒店","ListID":"","PayAmt":"","PayType":"","RommsCnt":"","SPTransID":"","State":""}]}}
构造对象:
public struct ToJsonMy3
{
public string result { get; set; }
public string res_info { get; set; }
public queryorder_info queryorder_info;
}
public struct queryorder_info
{
public string order_num { get; set; }
public List<orderdetail> orderdetail;//数组处理
};
public struct orderdetail
{
public string CFTUin { get; set; }
public string CancelDeadline { get; set; }
public string CheckInDate { get; set; }
public string CheckOutDate { get; set; }
public string CityID { get; set; }
public string CurrencyCode { get; set; }
public string HotelID { get; set; }
public string HotelName { get; set; }
public string ListID { get; set; }
public string PayAmt { get; set; }
public string PayType { get; set; }
public string RommsCnt { get; set; }
public string SPTransID { get; set; }
public string State { get; set; }
};
转换过程:
public static void JsonMy4()
{
string json = Jsonstr("D:\\json\\jsonmy4.txt");
JavaScriptSerializer js = new JavaScriptSerializer(); //实例化一个能够序列化数据的类
ToJsonMy3 list = js.Deserialize<ToJsonMy3>(json); //将json数据转化为对象类型并赋值给list
string result = list.result;
string res_info = list.res_info;
string order_num = list.queryorder_info.order_num;
List<orderdetail> orderdetail = list.queryorder_info.orderdetail;
string CFTUin = orderdetail[].CFTUin;
string HotelName = orderdetail[].HotelName;
string ListID = orderdetail[].ListID;
string State = orderdetail[].State;
}
以上代码来自:http://www.jb51.net/article/48027.htm
在项目中也遇到了使用JSON的情况,就把转化过程写一下。
1.从数据库获取的json数据为:
[{"ACTIONVALUE":"-1","CONTENT":"","DEVID":"","DEVNAME":"#1空调","DEVTYPE":"","EVENTTYPE":"","GRIDCODE":"","ISAUTO":"false","MEASURENAME":"通用.低风","MEASURETAG":"GEN.CtrlLowWind","STREAMTYPE":null,"TIMESPAN":""},{"ACTIONVALUE":"","CONTENT":"","DEVID":"","DEVNAME":"#1风机","DEVTYPE":"","EVENTTYPE":"","GRIDCODE":"","ISAUTO":"false","MEASURENAME":"风机开关","MEASURETAG":"GEN.UnibitCtrl","STREAMTYPE":null,"TIMESPAN":""},{"ACTIONVALUE":"","CONTENT":"","DEVID":"","DEVNAME":"#1警笛","DEVTYPE":"","EVENTTYPE":"","GRIDCODE":"","ISAUTO":"false","MEASURENAME":"警笛开关","MEASURETAG":"GEN.UnibitCtrl","STREAMTYPE":null,"TIMESPAN":""},{"ACTIONVALUE":"","CONTENT":"","DEVID":"","DEVNAME":"#1灯光","DEVTYPE":"","EVENTTYPE":"","GRIDCODE":"","ISAUTO":"true","MEASURENAME":"灯光开光","MEASURETAG":"GEN.UnibitCtrl","STREAMTYPE":null,"TIMESPAN":""},{"ACTIONVALUE":"","CONTENT":"","DEVID":"","DEVNAME":"hik1","DEVTYPE":"","EVENTTYPE":"","GRIDCODE":"","ISAUTO":"true","MEASURENAME":"","MEASURETAG":"GEN.PreSet","STREAMTYPE":"","TIMESPAN":""},{"ACTIONVALUE":"-1","CONTENT":"","DEVID":"","DEVNAME":"hik1","DEVTYPE":"","EVENTTYPE":"","GRIDCODE":"","ISAUTO":"true","MEASURENAME":"","MEASURETAG":"GEN.VideoRecord","STREAMTYPE":"","TIMESPAN":""}]
2.构造实体对象:
public class ActionParam
{
//[{"ACTIONVALUE":"1","CONTENT":"","DEVID":"30000449660708","DEVNAME":"#1灯光","DEVTYPE":"45","EVENTTYPE":"","GRIDCODE":"","ISAUTO":"true","MEASURENAME":"灯光开光","MEASURETAG":"GEN.UnibitCtrl","STREAMTYPE":null,"TIMESPAN":""}]
public int ACTIONVALUE { get; set; }
public string CONTENT { get; set; }
public string DEVID { get; set; }
public string DEVNAME { get; set; }
public string DEVTYPE { get; set; }
public string EVENTTYPE { get; set; }
public string GRIDCODE { get; set; }
public bool ISAUTO { get; set; }
public string MEASURENAME { get; set; }
public string MEASURETAG { get; set; }
public string STREAMTYPE { get; set; }
public string TIMESPAN { get; set; } }
3.转化为C#中声明的数据格式:
JavaScriptSerializer js = new JavaScriptSerializer(); //实例化一个能够序列化数据的类
List<ActionParam> listDetail = js.Deserialize<List<ActionParam>>(actionParam);
以为json字符串是数组格式,所以js.Deserialize<T>()泛型中使用List<ActionParam>.
C#处理JSON 数据的更多相关文章
- 使用TSQL查询和更新 JSON 数据
JSON是一个非常流行的,用于数据交换的文本数据(textual data)格式,主要用于Web和移动应用程序中.JSON 使用“键/值对”(Key:Value pair)存储数据,能够表示嵌套键值对 ...
- 利用Python进行数据分析(2) 尝试处理一份JSON数据并生成条形图
一.JSON 数据准备 首先准备一份 JSON 数据,这份数据共有 3560 条内容,每条内容结构如下: 本示例主要是以 tz(timezone 时区) 这一字段的值,分析这份数据里时区的分布情况. ...
- Salesforce Apex 使用JSON数据的示例程序
本文介绍了一个在Salesforce Apex中使用JSON数据的示例程序, 该示例程序由以下几部分组成: 1) Album.cls, 定了了封装相关字段的数据Model类 2) RestClient ...
- MVC使用ajax异步刷新时怎样输出从后台中传过来的JSON数据
前言 这几天在学习MVC使用AJAX异步刷,因为是新手.所以在js中传参数到后台以及后台返回数据到前台怎么接受,怎么前台遍历出JSON数据都开始不知道,相信新手在使用时跟我一样会遇到,这里我就和大家分 ...
- ASP.NET提取多层嵌套json数据的方法
本文实例讲述了ASP.NET利用第三方类库Newtonsoft.Json提取多层嵌套json数据的方法,具体例子如下. 假设需要提取的json字符串如下: {"name":&quo ...
- jQuey知识点三 解析json数据
1.解析简单数据 @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="vie ...
- JSON数据的使用
JSON (JavaScript Object Notation)一种简单的数据格式,比xml更轻巧. JSON 是 JavaScript 原生格式,这意味着在 JavaScript 中处理 JSON ...
- ajax获取json数据 for select2
json数据“a.json” [ { "id": "1", "text": "张三" }, { "id&quo ...
- Web API返回JSON数据
对Web API新手来说,不要忽略了ApiController 在web API中,方法的返回值如果是实体的话实际上是自动返回JSON数据的例如: 他的返回值就是这样的: { "Conten ...
- JSON数据和对象
在js中像数字类型.字符串类型.布尔类型这些都不能再被拆分,属于基本类型.与之相对有一种复杂类型:对象类型,它是本身由多个其他类型组合而成的. 创建对象有两种方法,一.new Object()创建一个 ...
随机推荐
- find、findIndex、indexOf、lastIndex、includes 数组五种查询条件方法介绍
find() 方法返回数组中满足提供的测试函数的第一个元素的值. 语法: arr.find(callback[, thisArg]) findIndex()方法返回数组中满足提供的测试函数的第一个元素 ...
- Html<img>标签特写 2017-03-10 AM
1.插入图片 <img src="picture1.gif" width="300" height="100" title=" ...
- Binary Tree Paths leetcode
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- USACO全部月赛及GateWay数据
月赛: 以07年open为例,网站如下 http://contest.usaco.org/OPEN07 其他的格式是http://contest.usaco.org/月份(月份的英文前三位,比如1月是 ...
- C++ Primer 5 CH1 开始
1.1 编写一个简单的C++程序 在大多数系统中,main 的返回值被用来指示状态.返回值 0 表示成功,非 0 的返回值的含义由系统定义,通常用来指出错误类型. 访问 main 的返回值的方法依赖于 ...
- 用js控制css属性
在用js控制css属性时,行内css属性可以任意控制,但若是在<style></style>中写的css属性,均不能用alert读取,但是赋值却有几种现象, 第一种:无法读取, ...
- Java中的常量治理
版权声明:本文为博主原创文章,转载请注明出处,欢迎使劲喷 虽然推崇在java中使用枚举(可查看<Java中的枚举的治理>)来对数据字典及常量进行控制,但是有些时候,我们还是会觉得常量控制更 ...
- MySQL二进制日志总结
二进制日志简单介绍 MySQL的二进制日志(binary log)是一个二进制文件,主要用于记录修改数据或有可能引起数据变更的MySQL语句.二进制日志(binary log)中记录了对MySQL数据 ...
- android使用TextView实现跑马灯的效果(1)
android使用TextView实现跑马灯的效果 1.activity_main.xml <?xml version="1.0" encoding="utf-8& ...
- Spring 框架原理
[spring框架原理] Spring框架原理 [博主]高瑞林 [博客地址]http://www.cnblogs.com/grl214 写给读者的话 ------亲爱的读者感谢您对小编的支持,当我正值 ...