c#处理3种json数据的实例
程序员文章站
2023-12-19 23:19:04
网络中数据传输经常是xml或者json,现在做的一个项目之前调其他系统接口都是返回的xml格式,刚刚遇到一个返回json格式数据的接口,通过例子由易到难总结一下处理过程,希...
网络中数据传输经常是xml或者json,现在做的一个项目之前调其他系统接口都是返回的xml格式,刚刚遇到一个返回json格式数据的接口,通过例子由易到难总结一下处理过程,希望能帮到和我一样开始不会的朋友。
一、c#处理简单json数据
json数据:
复制代码 代码如下:
{"result":"0","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;
}
{
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":"0","res_info":"ok","queryorder_info":{"order_num":"5","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;
}
{
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":"0","res_info":"ok","queryorder_info":{"order_num":"5","orderdetail":[{"cftuin":"769839263","canceldeadline":"2013-09-12 23:00:00","checkindate":"2013-09-12 00:00:00","checkoutdate":"2013-09-13 00:00:00","cityid":"0101","currencycode":"rmb","hotelid":"00301105","hotelname":"乐家连锁(北京天坛南门店)(原速8酒店(北京天坛南门店)","listid":"1000000005201308280002999652","payamt":"228","paytype":"0","rommscnt":"1","sptransid":"65202157","state":"4"},{"cftuin":"248486133","canceldeadline":"2013-10-13 23:00:00","checkindate":"2013-10-13 00:00:00","checkoutdate":"2013-10-18 00:00:00","cityid":"0201","currencycode":"rmb","hotelid":"10201314","hotelname":"上海凯顿酒店","listid":"1000000005201308280002999413","payamt":"1140","paytype":"0","rommscnt":"1","sptransid":"65197226","state":"4"}]}}
构造对象:
复制代码 代码如下:
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 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[0].cftuin;
string hotelname = orderdetail[0].hotelname;
string listid = orderdetail[1].listid;
string state = orderdetail[2].state;
}
{
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[0].cftuin;
string hotelname = orderdetail[0].hotelname;
string listid = orderdetail[1].listid;
string state = orderdetail[2].state;
}
ps:关于json操作,这里再为大家推荐几款比较实用的json在线工具供大家参考使用:
在线json代码检验、检验、美化、格式化工具:
json在线格式化工具:
在线xml/json互相转换工具:
json代码在线格式化/美化/压缩/编辑/转换工具:
在线json压缩/转义工具:
c语言风格/html/css/json代码格式化美化工具: