首页 > Web开发 > 详细

JSON字符串的生成

时间:2016-06-22 10:42:41      阅读:288      评论:0      收藏:0      [点我收藏+]
  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#";
            cop.@operator = "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";
                td.@operator = "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字符串的生成

原文:http://www.cnblogs.com/Nicolasap/p/5605853.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!