自己写的序列号数组为Json的方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ZmjTool
{
public class JsonTool
{
/// <summary>
/// 将一个列表转化为json字符串
/// </summary>
/// <param name="lst"></param>
/// <param name="havedm">指示是否含有双引号</param>
/// <param name="havebs">指示是否含有大括号</param>
/// <returns></returns>
public static string GetStrFromList<T>(List<T> lst, bool havedm = true, bool havebs = false)
{
if (lst == null) return "";
StringBuilder sb = new StringBuilder();
if (havebs) sb.Append('{');
sb.Append('[');
foreach (object item in lst)
{
if (havedm) sb.Append('"');
sb.Append(item?.ToString() ?? "");
if (havedm) sb.Append('"');
sb.Append(',');
}
if (sb[sb.Length - 2] == ',')
sb.Remove(sb.Length - 2, 2);//移除最后的,
sb.Append(']');
if (havebs) sb.Append('}');
return sb.ToString();
}
}
}