南骏 池
2024-12-21 d27ce4fecc8eaaf522353f972dc67a5d81b6ea2a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
 
namespace DataexchangeServer
{
    /// <summary>
    /// JSON帮助方法
    /// </summary>
    public class JSONHelper
    {
        /// <summary>
        /// 将json转换为DataTable
        /// </summary>
        /// <param name="strJson">得到的json</param>
        /// <returns></returns>
        public static DataTable JsonToDataTable(string strJson)
        {
            DataTable tb = null;
 
            if (strJson.IndexOf("\"|") > -1 || strJson.IndexOf("\"#+#") > -1)
            {
                return tb;
            }
            strJson = strJson.Replace("\",", "\"|").Replace("\":", "\"#+#").ToString();
 
            //取出表名   
            var rg = new Regex(@"(?<={)[^:]+(?=:\[)", RegexOptions.IgnoreCase);
            string strName = rg.Match(strJson).Value;
            //去除表名   
            strJson = strJson.Substring(strJson.IndexOf("[") + 1);
            strJson = strJson.Substring(0, strJson.IndexOf("]"));
 
            //获取数据   
            rg = new Regex(@"(?<={)[^}]+(?=})");
            MatchCollection mc = rg.Matches(strJson);
            for (int i = 0; i < mc.Count; i++)
            {
                string strRow = mc[i].Value;
                string[] strRows = strRow.Split('|');
 
                //创建表   
                if (tb == null)
                {
                    tb = new DataTable();
                    tb.TableName = strName;
                    foreach (string str in strRows)
                    {
                        var dc = new DataColumn();
                        string[] strCell = str.Split(new string[] { "#+#" }, StringSplitOptions.None);
 
                        if (strCell[0].Substring(0, 1) == "\"")
                        {
                            int a = strCell[0].Length;
                            dc.ColumnName = strCell[0].Substring(1, a - 2);
                        }
                        else
                        {
                            dc.ColumnName = strCell[0];
                        }
                        tb.Columns.Add(dc);
                    }
                    tb.AcceptChanges();
                }
 
                //增加内容   
                DataRow dr = tb.NewRow();
                for (int r = 0; r < strRows.Length; r++)
                {
                    dr[r] = strRows[r].Split(new string[] { "#+#" }, StringSplitOptions.None)[1].Trim().Replace(",", ",").Replace(":", ":").Replace("\"", "");
                }
                tb.Rows.Add(dr);
                tb.AcceptChanges();
            }
            return tb;
        }
 
        static object obj = new object();
        /// <summary>
        /// 写入文件
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static bool WriteToFile(string msg)
        {
            if (msg.Trim() == "") return true;
            try
            {
                lock (obj)
                {
 
                    string fileFolder = Environment.CurrentDirectory + @"\log\";
                    if (!Directory.Exists(fileFolder))
                    {
                        DirectoryInfo directoryInfo = new DirectoryInfo(fileFolder);
                        directoryInfo.Create();
                    }
 
                    string path = fileFolder + DateTime.Now.ToString("yyyyMMdd HH-mm") + ".txt";
                    using (FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write))
                    {
                        byte[] bytes = Encoding.Default.GetBytes(msg);
                        //fs.WriteTimeout = 10000;
                        fs.Write(bytes, 0, bytes.Length);
                        fs.Close();
                    }
                    return true;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
 
        }
 
 
    }
}