您的位置:首页 > Web前端 > JavaScript

【测试Json的多空格问题】

2016-05-27 16:10 579 查看
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using Newtonsoft.Json;

namespace WindowsFormsAppDemoForJson
{
public partial class Form1 : Form
{

private Random _randor;
private readonly char[] BLANKCHARS = new char[]
{
(char) 0x20,
(char) 0x20,
(char) 0x20,
(char) 0x20,
(char) 0x20,
(char) 0x20,
(char) 0x20,
(char) 0x20,
(char) 0x20,
(char) 0x20,
(char) 0x20,
(char) 0x20,
};
public Form1()
{
InitializeComponent();

_randor = new Random(DateTime.Now.Millisecond);
}

private List<Product> InitData()
{
var ls_data = new List<Product>();

var blank = this.txt_BlankCount.Text.Trim();
var blankCount = 0;
if (!string.IsNullOrEmpty(blank))
{
int.TryParse(blank, out blankCount);

}
if (blankCount > 10)
{
throw new Exception("不要超出10个空格!");
}

var spilitOfBlank = new string(this.BLANKCHARS.Take(blankCount).ToArray());

//生成5条数据
for (int i = 0; i < 5; i++)
{
var model = new Product()
{
Id = this._randor.Next(101, 107)

};

model.Title = string.Format("{0}-{1}-{0}", (char)model.Id, spilitOfBlank);

ls_data.Add(model);
}

return ls_data;
}

private void btn_Serize_Click(object sender, EventArgs e)
{
var data = this.InitData();

var result = JsonConvert.SerializeObject(data);

this.txtResult.Text = result;
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: