JSON字符串的生成
Posted Nicolasap
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JSON字符串的生成相关的知识,希望对你有一定的参考价值。
public class Corporation {
public string remark { get; set; } public string version { get; set; } public string @operator { get; set; } }
public class TenderDoc { public string tenderdocid { get; set; } public Corporation corporation { get; set; } public string context { get; set; }public string @operator { get; set; } }
public class Root { public List<TenderDoc> datas { get; set; } public string flag{ get; set; } }
这样就定义了json的实体类。
Corporation cop = new Corporation(); cop.remark = "remark"; cop.version = "C#"; [email protected] = "niap"; List<TenderDoc> listtd = new List<TenderDoc>(); for (int i = 0; i < 2; i++) { TenderDoc td = new TenderDoc(); td.tenderdocid = "NO1"; td.corporation = cop; td.context = "context"; [email protected] = "miap"; listtd.Add(td); } Root root = new Root() { datas = listtd, flag = "001" }; string jsonStr = JsonConvert.SerializeObject(root); Response.Write(jsonStr);
最后调用 JsonConvert.SerializeObject(),就可以将实体类对象转为JSON。
转换结果如下
{ "datas": [ { "tenderdocid": "NO1", "corporation": { "remark": "remark", "version": "C#", "operator": "niap" }, "context": "context", "operator": "miap" }, { "tenderdocid": "NO1", "corporation": { "remark": "remark", "version": "C#", "operator": "niap" }, "context": "context", "operator": "miap" } ], "flag": "001" }
以上是关于JSON字符串的生成的主要内容,如果未能解决你的问题,请参考以下文章
vscode 用户代码片段 vue初始化模板 Snippet #新加入开头注释 自动生成文件名 开发日期时间等内容