C# json转对象
Posted hofmann
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# json转对象相关的知识,希望对你有一定的参考价值。
EnterpriseResponse09.json
{ "response_biz_content": { "MSG": "OK", "return_msg": "OK", "msg_id": "637067564513056852", "DATA": [{ "SIZE": 1, "TCONF": { "C_EFFECTIVE_FLAG": "案件状态", "C_CASE_CODE": "案号", "C_SEX_NAME": "性别", "C_AREA_NAME": "地域名称", "C_GIST_UNIT": "作出执行依据单位", "C_BUESINESSENTITY": "企业法人姓名", "N_PARTY_TYPE_NAME": "标识自然人或企业法人", "D_DATA_DATE": "数据日期", "UNPERFORM_PART": "未履行部分", "N_AGE": "年龄", "PERFORMED_PART": "已履行部分", "C_PERFORMANCE": "被执行人的履行情况", "D_REG_DATE": "立案时间", "DUTY": "法律生效文书确定的义务", "C_INAME": "失信被执行人姓名/名称", "D_PUBLISH_DATE": "发布时间", "C_CARDNUM": "身份证号码/组织机构代码", "C_DISREPUT_TYPE_NAME": "失信被执行人具体情形", "C_COURT_NAME": "执行法院", "N_AREA_ID": "地域ID", "C_GIST_CID": "执行依据文号" }, "MODEL": "09", "RISKTYPE": "失信被执行人", "RISKTYPEDES": "存在通过各种方法拒不执行法院判决的行为,俗称“老赖”。", "TOTALCOUNT": 1, "TDATA": [{ "C_EFFECTIVE_FLAG": "1", "C_CASE_CODE": "(2008)沪213232法执字第01109号", "C_SEX_NAME": "", "C_AREA_NAME": "上海", "C_GIST_UNIT": "厦门市集美区人民法院", "C_BUESINESSENTITY": "李国华", "N_PARTY_TYPE_NAME": "", "D_DATA_DATE": "20190816", "UNPERFORM_PART": "", "N_AGE": "0", "PERFORMED_PART": "", "C_PERFORMANCE": "全部未履行", "D_REG_DATE": "20080902", "DUTY": "", "C_INAME": "79757239202-0", "D_PUBLISH_DATE": "20150106", "C_CARDNUM": "", "C_DISREPUT_TYPE_NAME": "其他有履行能力而拒不履行生效法律文书确定义务", "C_COURT_NAME": "上海市浦东新区人民法院", "N_AREA_ID": 672, "C_GIST_CID": "(2008)沪法执字第01109号" }] }], "return_code": "0000", "CODE": "02-000-203-0000" }, "sign": "H8ESG0qXjYqZaA+rVcY0akVItL74kP11Mb2N4ZaABqL6/FZhU4HfxxKnn9NKU/DDbuIms7Udc89hqmWCzUMDIgKmbisvqimIddjj1RcLNEtiIxgbSwbCmb1KeEFw9gSZiS+7T6kzA86LuFw1IlJRIxU3DBxPzArY88+6RyZmyBQ=" }
TestResponse.cs
using System; using System.Runtime.CompilerServices; using System.Runtime.Serialization; [Serializable, DataContract] public class IcbcResponse { [DataMember] private int return_code = -1; [DataMember] private string return_msg = "unexpected error."; [DataMember] private string msg_id; public string getMsgId() { return this.msg_id; } public int getReturnCode() { return this.return_code; } public string getReturnMsg() { return this.return_msg; } public bool isSuccess() { return (this.return_code == 0); } public void setMsgId(string value) { this.msg_id = value; } public void setReturnCode(int value) { this.return_code = value; } public void setReturnMsg(string value) { this.return_msg = value; } [DataMember] public string MSG { get; set; } [DataMember] public string CODE { get; set; } }
EnterpriseResponse09.cs
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.Text; using System.Threading.Tasks; namespace Entity.Response.Enterprise { public class EnterpriseResponse09 : TestResponse { [DataMember] public List<DATAItem> DATA { get; set; } /// <summary> /// 数据属性 /// </summary> public class DATAItem { /// <summary> /// 数量 /// </summary> public int SIZE { get; set; } /// <summary> /// 列名 /// </summary> public TCONF TCONF { get; set; } /// <summary> /// 关联服务查询类型 /// </summary> public string MODEL { get; set; } /// <summary> /// 失信被执行人 /// </summary> public string RISKTYPE { get; set; } /// <summary> /// 存在通过各种方法拒不执行法院判决的行为,俗称“老赖”。 /// </summary> public string RISKTYPEDES { get; set; } /// <summary> /// 数量 /// </summary> public int TOTALCOUNT { get; set; } /// <summary> /// 数据 /// </summary> public List<TDATAItem> TDATA { get; set; } /// <summary> /// 数据 /// </summary> public class TDATAItem { /// <summary> /// 案号|Case Number /// </summary> public string C_CASE_CODE { get; set; } /// <summary> /// 失信被执行人姓名/名称|Name/Designation /// </summary> public string C_INAME { get; set; } /// <summary> /// 性别|Gender /// </summary> public string C_SEX_NAME { get; set; } /// <summary> /// 年龄|Age /// </summary> public string N_AGE { get; set; } /// <summary> /// 身份证号码/组织机构代码|ID card No./Org code /// </summary> public string C_CARDNUM { get; set; } /// <summary> /// 企业法人姓名|Name of legal person /// </summary> public string C_BUESINESSENTITY { get; set; } /// <summary> /// 执行法院|The Court /// </summary> public string C_COURT_NAME { get; set; } /// <summary> /// 地域ID|Region ID /// </summary> public string N_AREA_ID { get; set; } /// <summary> /// 地域名称|Region name /// </summary> public string C_AREA_NAME { get; set; } /// <summary> /// 标识自然人或企业法人|Natural/legal person /// </summary> public string N_PARTY_TYPE_NAME { get; set; } /// <summary> /// 执行依据文号|Exec. basis doc No. /// </summary> public string C_GIST_CID { get; set; } /// <summary> /// 作出执行依据单位|Unit of execution basis /// </summary> public string C_GIST_UNIT { get; set; } /// <summary> /// 法律生效文书确定的义务|Obligation of legal doc /// </summary> public string DUTY { get; set; } /// <summary> /// 被执行人的履行情况|Perf. of executed /// </summary> public string C_PERFORMANCE { get; set; } /// <summary> /// 失信被执行人具体情形|Situation of executed /// </summary> public string C_DISREPUT_TYPE_NAME { get; set; } /// <summary> /// 发布时间|Time of release /// </summary> public string D_PUBLISH_DATE { get; set; } /// <summary> /// 立案时间|Time of case filing /// </summary> public string D_REG_DATE { get; set; } /// <summary> /// 已履行部分|Performed /// </summary> public string PERFORMED_PART { get; set; } /// <summary> /// 未履行部分|To be performed /// </summary> public string UNPERFORM_PART { get; set; } /// <summary> /// 案件状态|Case status /// </summary> public string C_EFFECTIVE_FLAG { get; set; } /// <summary> /// 数据日期 /// </summary> public string D_DATA_DATE { get; set; } } } /// <summary> /// 数据描述 /// </summary> public class TCONF { /// <summary> /// 案件状态 /// </summary> public string C_EFFECTIVE_FLAG { get; set; } /// <summary> /// 案号 /// </summary> public string C_CASE_CODE { get; set; } /// <summary> /// 性别 /// </summary> public string C_SEX_NAME { get; set; } /// <summary> /// 地域名称 /// </summary> public string C_AREA_NAME { get; set; } /// <summary> /// 作出执行依据单位 /// </summary> public string C_GIST_UNIT { get; set; } /// <summary> /// 企业法人姓名 /// </summary> public string C_BUESINESSENTITY { get; set; } /// <summary> /// 标识自然人或企业法人 /// </summary> public string N_PARTY_TYPE_NAME { get; set; } /// <summary> /// 数据日期 /// </summary> public string D_DATA_DATE { get; set; } /// <summary> /// 未履行部分 /// </summary> public string UNPERFORM_PART { get; set; } /// <summary> /// 年龄 /// </summary> public string N_AGE { get; set; } /// <summary> /// 已履行部分 /// </summary> public string PERFORMED_PART { get; set; } /// <summary> /// 被执行人的履行情况 /// </summary> public string C_PERFORMANCE { get; set; } /// <summary> /// 立案时间 /// </summary> public string D_REG_DATE { get; set; } /// <summary> /// 法律生效文书确定的义务 /// </summary> public string DUTY { get; set; } /// <summary> /// 失信被执行人姓名/名称 /// </summary> public string C_INAME { get; set; } /// <summary> /// 发布时间 /// </summary> public string D_PUBLISH_DATE { get; set; } /// <summary> /// 身份证号码/组织机构代码 /// </summary> public string C_CARDNUM { get; set; } /// <summary> /// 失信被执行人具体情形 /// </summary> public string C_DISREPUT_TYPE_NAME { get; set; } /// <summary> /// 执行法院 /// </summary> public string C_COURT_NAME { get; set; } /// <summary> /// 地域ID /// </summary> public string N_AREA_ID { get; set; } /// <summary> /// 执行依据文号 /// </summary> public string C_GIST_CID { get; set; } } } }
DataContractJsonSerializer
string baseDirectory = AppDomain.CurrentDomain.BaseDirectory; string jsonfile = Path.Combine(baseDirectory, "EnterpriseResponse09.json"); string responseJson = string.Empty; using (StreamReader r = new StreamReader(jsonfile)) { responseJson = r.ReadToEnd(); } string response_biz_content = "response_biz_content"; int startIndex = (responseJson.IndexOf(response_biz_content) + response_biz_content.Length) + 2; string content = responseJson.Substring(startIndex, responseJson.LastIndexOf(",") - startIndex); EnterpriseResponse09 response = null; using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(content))) { response = (EnterpriseResponse09)new DataContractJsonSerializer(typeof(EnterpriseResponse09)).ReadObject(stream); } //responseJson = "{"response_biz_content":{"MSG":"OK","return_msg":"OK","msg_id":"637067564513056852","DATA":[{"SIZE":1,"TCONF":{"C_EFFECTIVE_FLAG":"案件状态","C_CASE_CODE":"案号","C_SEX_NAME":"性别","C_AREA_NAME":"地域名称","C_GIST_UNIT":"作出执行依据单位","C_BUESINESSENTITY":"企业法人姓名","N_PARTY_TYPE_NAME":"标识自然人或企业法人","D_DATA_DATE":"数据日期","UNPERFORM_PART":"未履行部分","N_AGE":"年龄","PERFORMED_PART":"已履行部分","C_PERFORMANCE":"被执行人的履行情况","D_REG_DATE":"立案时间","DUTY":"法律生效文书确定的义务","C_INAME":"失信被执行人姓名/名称","D_PUBLISH_DATE":"发布时间","C_CARDNUM":"身份证号码/组织机构代码","C_DISREPUT_TYPE_NAME":"失信被执行人具体情形","C_COURT_NAME":"执行法院","N_AREA_ID":"地域ID","C_GIST_CID":"执行依据文号"},"MODEL":"09","RISKTYPE":"失信被执行人","RISKTYPEDES":"存在通过各种方法拒不执行法院判决的行为,俗称“老赖”。","TOTALCOUNT":1,"TDATA":[{"C_EFFECTIVE_FLAG":"1","C_CASE_CODE":"(2008)沪213232法执字第01109号","C_SEX_NAME":"","C_AREA_NAME":"上海","C_GIST_UNIT":"厦门市集美区人民法院","C_BUESINESSENTITY":"李国华","N_PARTY_TYPE_NAME":"","D_DATA_DATE":"20190816","UNPERFORM_PART":"","N_AGE":"0","PERFORMED_PART":"","C_PERFORMANCE":"全部未履行","D_REG_DATE":"20080902","DUTY":"","C_INAME":"79757239202-0","D_PUBLISH_DATE":"20150106","C_CARDNUM":"","C_DISREPUT_TYPE_NAME":"其他有履行能力而拒不履行生效法律文书确定义务","C_COURT_NAME":"上海市浦东新区人民法院","N_AREA_ID":672,"C_GIST_CID":"(2008)沪法执字第01109号"}]}],"return_code":"0000","CODE":"02-000-203-0000"},"sign":"H8ESG0qXjYqZaA+rVcY0akVItL74kP11Mb2N4ZaABqL6/FZhU4HfxxKnn9NKU/DDbuIms7Udc89hqmWCzUMDIgKmbisvqimIddjj1RcLNEtiIxgbSwbCmb1KeEFw9gSZiS+7T6kzA86LuFw1IlJRIxU3DBxPzArY88+6RyZmyBQ="}";
以上是关于C# json转对象的主要内容,如果未能解决你的问题,请参考以下文章