本次工作内容是要将以下数据解析成.Net可以使用的数据,返回的数据除了header,其他的都是可变的,也就是说结构不是固定的。完全由用户选择,所以选择了生成DataTable。
Json数据格式如下:
复制代码 代码如下:
{"dataSet":{
"header":{
"returnCode":"0",
"errorInfo":"HTTP请求错误",
"version":"V1.0R010",
"totalRows":"2000",
"returnRows":"20"
},
"fieldDefine":{
"assetId":"string",
"serverIdcId":"int",
"inputTime":"datetime"
},
"data":{"row":[
{
"AssetId":"TCNS2006888",
"ServerIdcId":"1",
"InputTime":"2008-12-12"
},
{
"AssetId":"TCNS2006889",
"ServerIdcId":"2",
"InputTime":"2008-1-1"
}
]}
}
}
解析的类:
复制代码 代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web.Script.Serialization;
namespace Tencent.Itil.Cmsi.Common
{
public class GeneralSearchResult
{
public Header header = new Header();
private DataTable fieldDefine = new DataTable();
/// <summary>
/// 返回的数据结构定义,无数据
/// </summary>
public DataTable FieldDefine
{
get { return fieldDefine; }
set { fieldDefine = value; }
}
private DataTable retrunData = new DataTable();
/// <summary>
/// 返回的数据,格式为DataTable,结构和FieldDefine中的结构一样
/// </summary>
public DataTable RetrunData
{
get { return retrunData; }
set { retrunData = value; }
}
/// <summary>
/// 将json数据转换为定义好的对象,数据转换为DataTable
/// </summary>
/// <param name="jsonText"></param>
/// <returns></returns>
public static GeneralSearchResult GetTransformData(string jsonText)
{
GeneralSearchResult gsr = new GeneralSearchResult();
JavaScriptSerializer s = new JavaScriptSerializer();
Dictionary<string, object> JsonData = (Dictionary<string, object>)s.DeserializeObject(jsonText);
Dictionary<string, object> dataSet = (Dictionary<string, object>)JsonData["dataSet"];
Dictionary<string, object> header = (Dictionary<string, object>)dataSet["header"];
Dictionary<string, object> fieldDefine = (Dictionary<string, object>)dataSet["header"];
Dictionary<string, object> data = (Dictionary<string, object>)dataSet["data"];
object[] rows = (object[])data["row"];
gsr.header.Version = header["version"].ToString();
gsr.header.ErrorInfo = header["errorInfo"].ToString();
gsr.header.ReturnCode = header["returnCode"].ToString();
gsr.header.ReturnRows = Convert.ToInt16(header["returnRows"]);
gsr.header.TotalRows = Convert.ToInt16(header["totalRows"]);
Dictionary<string, object> dicFieldDefine = (Dictionary<string, object>)dataSet["fieldDefine"];
foreach (KeyValuePair<string, object> ss in dicFieldDefine)
{
gsr.FieldDefine.Columns.Add(ss.Key, typeof(string));
}
gsr.RetrunData = gsr.FieldDefine.Clone();
foreach (object ob in rows)
{
Dictionary<string, object> val = (Dictionary<string, object>)ob;
DataRow dr = gsr.RetrunData.NewRow();
foreach (KeyValuePair<string, object> sss in val)
{
dr[sss.Key] = sss.Value;
}
gsr.RetrunData.Rows.Add(dr);
}
return gsr;
}
/// <summary>
/// 数据文件头定义
/// </summary>
public class Header
{
private string version;
/// <summary>
/// 版本
/// </summary>
public string Version
{
get { return version; }
set { version = value; }
}
private string returnCode;
/// <summary>
/// 结果码,0为正常,否则为有错误
/// </summary>
public string ReturnCode
{
get { return returnCode; }
set { returnCode = value; }
}
private string errorInfo;
/// <summary>
/// 如果ReturnCode为非0时的错误信息
/// </summary>
public string ErrorInfo
{
get { return errorInfo; }
set { errorInfo = value; }
}
private int totalRows;
/// <summary>
/// 查询结果总行数
/// </summary>
public int TotalRows
{
get { return totalRows; }
set { totalRows = value; }
}
private int returnRows;
/// <summary>
/// 返回的数据行数
/// </summary>
public int ReturnRows
{
get { return returnRows; }
set { returnRows = value; }
}
}
}
}
使用方法:
GeneralSearchResult gsr = new GeneralSearchResult();
gsr = GeneralSearchResult.GetTransformData(text);
Json数据格式如下:
复制代码 代码如下:
{"dataSet":{
"header":{
"returnCode":"0",
"errorInfo":"HTTP请求错误",
"version":"V1.0R010",
"totalRows":"2000",
"returnRows":"20"
},
"fieldDefine":{
"assetId":"string",
"serverIdcId":"int",
"inputTime":"datetime"
},
"data":{"row":[
{
"AssetId":"TCNS2006888",
"ServerIdcId":"1",
"InputTime":"2008-12-12"
},
{
"AssetId":"TCNS2006889",
"ServerIdcId":"2",
"InputTime":"2008-1-1"
}
]}
}
}
解析的类:
复制代码 代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web.Script.Serialization;
namespace Tencent.Itil.Cmsi.Common
{
public class GeneralSearchResult
{
public Header header = new Header();
private DataTable fieldDefine = new DataTable();
/// <summary>
/// 返回的数据结构定义,无数据
/// </summary>
public DataTable FieldDefine
{
get { return fieldDefine; }
set { fieldDefine = value; }
}
private DataTable retrunData = new DataTable();
/// <summary>
/// 返回的数据,格式为DataTable,结构和FieldDefine中的结构一样
/// </summary>
public DataTable RetrunData
{
get { return retrunData; }
set { retrunData = value; }
}
/// <summary>
/// 将json数据转换为定义好的对象,数据转换为DataTable
/// </summary>
/// <param name="jsonText"></param>
/// <returns></returns>
public static GeneralSearchResult GetTransformData(string jsonText)
{
GeneralSearchResult gsr = new GeneralSearchResult();
JavaScriptSerializer s = new JavaScriptSerializer();
Dictionary<string, object> JsonData = (Dictionary<string, object>)s.DeserializeObject(jsonText);
Dictionary<string, object> dataSet = (Dictionary<string, object>)JsonData["dataSet"];
Dictionary<string, object> header = (Dictionary<string, object>)dataSet["header"];
Dictionary<string, object> fieldDefine = (Dictionary<string, object>)dataSet["header"];
Dictionary<string, object> data = (Dictionary<string, object>)dataSet["data"];
object[] rows = (object[])data["row"];
gsr.header.Version = header["version"].ToString();
gsr.header.ErrorInfo = header["errorInfo"].ToString();
gsr.header.ReturnCode = header["returnCode"].ToString();
gsr.header.ReturnRows = Convert.ToInt16(header["returnRows"]);
gsr.header.TotalRows = Convert.ToInt16(header["totalRows"]);
Dictionary<string, object> dicFieldDefine = (Dictionary<string, object>)dataSet["fieldDefine"];
foreach (KeyValuePair<string, object> ss in dicFieldDefine)
{
gsr.FieldDefine.Columns.Add(ss.Key, typeof(string));
}
gsr.RetrunData = gsr.FieldDefine.Clone();
foreach (object ob in rows)
{
Dictionary<string, object> val = (Dictionary<string, object>)ob;
DataRow dr = gsr.RetrunData.NewRow();
foreach (KeyValuePair<string, object> sss in val)
{
dr[sss.Key] = sss.Value;
}
gsr.RetrunData.Rows.Add(dr);
}
return gsr;
}
/// <summary>
/// 数据文件头定义
/// </summary>
public class Header
{
private string version;
/// <summary>
/// 版本
/// </summary>
public string Version
{
get { return version; }
set { version = value; }
}
private string returnCode;
/// <summary>
/// 结果码,0为正常,否则为有错误
/// </summary>
public string ReturnCode
{
get { return returnCode; }
set { returnCode = value; }
}
private string errorInfo;
/// <summary>
/// 如果ReturnCode为非0时的错误信息
/// </summary>
public string ErrorInfo
{
get { return errorInfo; }
set { errorInfo = value; }
}
private int totalRows;
/// <summary>
/// 查询结果总行数
/// </summary>
public int TotalRows
{
get { return totalRows; }
set { totalRows = value; }
}
private int returnRows;
/// <summary>
/// 返回的数据行数
/// </summary>
public int ReturnRows
{
get { return returnRows; }
set { returnRows = value; }
}
}
}
}
使用方法:
GeneralSearchResult gsr = new GeneralSearchResult();
gsr = GeneralSearchResult.GetTransformData(text);
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
暂无评论...
更新日志
2024年11月29日
2024年11月29日
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]