asp.net(C#)解析Json的类代码
程序员文章站
2024-03-09 09:30:05
本次工作内容是要将以下数据解析成.net可以使用的数据,返回的数据除了header,其他的都是可变的,也就是说结构不是固定的。完全由用户选择,所以选择了生成datatabl...
本次工作内容是要将以下数据解析成.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);