1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Threading.Tasks;
9 using System.Windows.Forms;
10
11 using Newtonsoft.Json;
12
13
14 namespace WindowsFormsAppDemoForJson
15 {
16 public partial class Form1 : Form
17 {
18
19
20 private Random _randor;
21 private readonly char[] BLANKCHARS = new char[]
22 {
23 (char) 0x20,
24 (char) 0x20,
25 (char) 0x20,
26 (char) 0x20,
27 (char) 0x20,
28 (char) 0x20,
29 (char) 0x20,
30 (char) 0x20,
31 (char) 0x20,
32 (char) 0x20,
33 (char) 0x20,
34 (char) 0x20,
35 };
36 public Form1()
37 {
38 InitializeComponent();
39
40 _randor = new Random(DateTime.Now.Millisecond);
41 }
42
43 private List<Product> InitData()
44 {
45 var ls_data = new List<Product>();
46
47 var blank = this.txt_BlankCount.Text.Trim();
48 var blankCount = 0;
49 if (!string.IsNullOrEmpty(blank))
50 {
51 int.TryParse(blank, out blankCount);
52
53
54 }
55 if (blankCount > 10)
56 {
57 throw new Exception("不要超出10个空格!");
58 }
59
60
61
62 var spilitOfBlank = new string(this.BLANKCHARS.Take(blankCount).ToArray());
63
64 //生成5条数据
65 for (int i = 0; i < 5; i++)
66 {
67 var model = new Product()
68 {
69 Id = this._randor.Next(101, 107)
70
71 };
72
73 model.Title = string.Format("{0}-{1}-{0}", (char)model.Id, spilitOfBlank);
74
75 ls_data.Add(model);
76 }
77
78
79 return ls_data;
80 }
81
82 private void btn_Serize_Click(object sender, EventArgs e)
83 {
84 var data = this.InitData();
85
86 var result = JsonConvert.SerializeObject(data);
87
88
89 this.txtResult.Text = result;
90 }
91
92
93 }
94 }