以前就遇到过,当时代码量不是很大,就手写了实体类。当时就想写个工具来自动生成。但是嫌麻烦了。感觉也不好写。
最近写个页游的辅助,返回的是Json,上百个不同的JSON返回值。如果光写实体类,我估计我不用干别的了。
下定决心写这么个工具,仔细研究了下Newtonsoft.Json.dll 发现用这个来写的话。就方便了很多了。
后发现老外写了个更好用的。(开源)
http://www.xamasoft.com/json-class-generator/
我的就直接淘汰掉了。
天朝的局域网太强大了。下载得去CodePlex。
http://jsonclassgenerator.codeplex.com/releases/view/102614
源码:http://jsonclassgenerator.codeplex.com/SourceControl/latest
支持生成C#和VB.NET实体
以1楼给我的例子。做一个演示。
{
"chart": {
"manageresize": "1",
"dateformat": "mm/dd/yyyy",
"outputdateformat": "hh:mn",
"caption": "Work Schedule",
"subcaption": "For next 2 days"
},
"categories": [
{
"category": [
{
"start": "7/15/2007 00:00:00",
"end": "7/15/2007 23:59:59",
"label": "Today"
},
{
"start": "7/16/2007 00:00:00",
"end": "7/16/2007 23:59:59",
"label": "Tomorrow"
}
]
},
{
"align": "right",
"category": [
{
"start": "7/15/2007 00:00:00",
"end": "7/15/2007 05:59:59",
"label": "6 am"
},
{
"start": "7/15/2007 06:00:00",
"end": "7/15/2007 11:59:59",
"label": "12 pm"
},
{
"start": "7/15/2007 12:00:00",
"end": "7/15/2007 17:59:59",
"label": "6 pm"
},
{
"start": "7/15/2007 18:00:00",
"end": "7/15/2007 23:59:59",
"label": "Midnight"
},
{
"start": "7/16/2007 00:00:00",
"end": "7/16/2007 05:59:59",
"label": "6 am"
},
{
"start": "7/16/2007 06:00:00",
"end": "7/16/2007 11:59:59",
"label": "12 pm"
}
]
}
],
"processes": {
"fontsize": "12",
"isbold": "1",
"align": "left",
"headertext": "Who?",
"headerfontsize": "18",
"headervalign": "bottom",
"headeralign": "left",
"process": [
{
"label": "John.S",
"id": "EMP121"
},
{
"label": "David.G",
"id": "EMP122"
},
{
"label": "Mary.P",
"id": "EMP123"
}
]
},
"tasks": {
"showlabels": "1",
"showstartdate": "1",
"task": [
{
"processid": "EMP121",
"start": "7/15/2007 08:00:00",
"end": "7/15/2007 12:30:00",
"label": "Logo A"
},
{
"processid": "EMP121",
"start": "7/15/2007 17:00:00",
"end": "7/15/2007 19:30:00",
"label": "Logo B"
},
{
"processid": "EMP121",
"start": "7/16/2007 09:00:00",
"end": "7/16/2007 19:30:00",
"label": "Website template"
},
{
"processid": "EMP122",
"start": "7/15/2007 10:00:00",
"end": "7/15/2007 20:30:00",
"label": "PHP Blueprint"
}
]
}
}
转换后的C# Model
点击paste & go,转换后的实体类复制到了剪辑版中。CTRL+V即可。
// Generated by Xamasoft JSON Class Generator
// http://www.xamasoft.com/json-class-generator
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Example
{
public partial class SampleResponse1
{
public class Chart2
{
[JsonProperty("manageresize")]
public string Manageresize { get; set; }
[JsonProperty("dateformat")]
public string Dateformat { get; set; }
[JsonProperty("outputdateformat")]
public string Outputdateformat { get; set; }
[JsonProperty("caption")]
public string Caption { get; set; }
[JsonProperty("subcaption")]
public string Subcaption { get; set; }
}
}
public partial class SampleResponse1
{
public class Category2
{
[JsonProperty("start")]
public string Start { get; set; }
[JsonProperty("end")]
public string End { get; set; }
[JsonProperty("label")]
public string Label { get; set; }
}
}
public partial class SampleResponse1
{
public class Category
{
[JsonProperty("category")]
public Category2[] Category { get; set; }
[JsonProperty("align")]
public string Align { get; set; }
}
}
public partial class SampleResponse1
{
public class Process2
{
[JsonProperty("label")]
public string Label { get; set; }
[JsonProperty("id")]
public string Id { get; set; }
}
}
public partial class SampleResponse1
{
public class Processes2
{
[JsonProperty("fontsize")]
public string Fontsize { get; set; }
[JsonProperty("isbold")]
public string Isbold { get; set; }
[JsonProperty("align")]
public string Align { get; set; }
[JsonProperty("headertext")]
public string Headertext { get; set; }
[JsonProperty("headerfontsize")]
public string Headerfontsize { get; set; }
[JsonProperty("headervalign")]
public string Headervalign { get; set; }
[JsonProperty("headeralign")]
public string Headeralign { get; set; }
[JsonProperty("process")]
public Process2[] Process { get; set; }
}
}
public partial class SampleResponse1
{
public class Task2
{
[JsonProperty("processid")]
public string Processid { get; set; }
[JsonProperty("start")]
public string Start { get; set; }
[JsonProperty("end")]
public string End { get; set; }
[JsonProperty("label")]
public string Label { get; set; }
}
}
public partial class SampleResponse1
{
public class Tasks2
{
[JsonProperty("showlabels")]
public string Showlabels { get; set; }
[JsonProperty("showstartdate")]
public string Showstartdate { get; set; }
[JsonProperty("task")]
public Task2[] Task { get; set; }
}
}
public partial class SampleResponse1
{
[JsonProperty("chart")]
public Chart2 Chart { get; set; }
[JsonProperty("categories")]
public Category[] Categories { get; set; }
[JsonProperty("processes")]
public Processes2 Processes { get; set; }
[JsonProperty("tasks")]
public Tasks2 Tasks { get; set; }
}
}