• 【测试Json的多空格问题】


     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 }
  • 相关阅读:
    基于php5.5使用PHPMailer-5.2发送邮件
    centos安装memcached和PHP php-pecl-memcached.x86_64
    修改覆盖springboot默认日志策略logback
    Springboot打包支持第三方jar
    Intellij IDEA 快速添加maven依赖
    Mybatis Generator生成Example类方法解释
    MyBatis generator生成的Example文件用法
    Intellij IDEA 中怎么把这个黄色的填充色取消?
    MyBatis自动生成工具去掉注释的配置
    去掉Mybatis Generator生成的example
  • 原文地址:https://www.cnblogs.com/micro-chen/p/5534995.html
Copyright © 2020-2023  润新知