コード例 #1
0
ファイル: calcmark.cs プロジェクト: eddylin2015/mklib
        public static void outputJSON(DataSet ds, string pclassno, string term, string mg, string sess, string session, string pdate, string filter, TextWriter sw, bool namemask = false)
        {
            string          pattern = @"\d+";
            Regex           rx      = new Regex(pattern);
            MatchCollection matches = rx.Matches(filter);

            sw.WriteLine("[");
            DataTable tb     = ds.Tables["Table"];
            int       rowcnt = 0;

            for (int i = 0; i < tb.Rows.Count; i++)
            {
                //選擇輸出:1,2,3,4
                string seat = tb.Rows[i]["curr_seat"].ToString();
                if (matches.Count > 0)
                {
                    int crt = 0;
                    for (crt = 0; crt < matches.Count; crt++)
                    {
                        if (matches[crt].Value.Equals(seat))
                        {
                            break;
                        }
                    }
                    if (crt >= matches.Count)
                    {
                        continue;
                    }
                }

                if (rowcnt > 0)
                {
                    sw.WriteLine(",");
                }
                rowcnt++;
                sw.WriteLine("{");
                for (int j = 0; j < tb.Columns.Count; j++)
                {
                    if (j > 0)
                    {
                        sw.WriteLine(",");
                    }
                    if (namemask && (tb.Columns[j].ColumnName.Equals("stud_ref") || tb.Columns[j].ColumnName.Equals("dsej_ref") || tb.Columns[j].ColumnName.Equals("c_name") || tb.Columns[j].ColumnName.Equals("e_name") || tb.Columns[j].ColumnName.Equals("curr_class")))
                    {
                        sw.Write("{0}:\"{1}\"", tb.Columns[j].ColumnName, "---");
                    }
                    else
                    {
                        sw.Write("{0}:\"{1}\"", tb.Columns[j].ColumnName, tb.Rows[i][j]);
                    }
                }
                foreach (DataRow cRow in tb.Rows[i].GetChildRows("sr_py"))
                {
                    for (int j = 0; j < cRow.Table.Columns.Count; j++)
                    {
                        if (!cRow.Table.Columns[j].ColumnName.Equals("stud_ref"))
                        {
                            sw.Write(",{0}:\"{1}\"", cRow.Table.Columns[j].ColumnName, MK_Utils.str2html(cRow[j].ToString()));
                        }
                    }
                }
                foreach (DataRow cRow in tb.Rows[i].GetChildRows("sr_cd"))
                {
                    for (int j = 0; j < cRow.Table.Columns.Count; j++)
                    {
                        if (!cRow.Table.Columns[j].ColumnName.Equals("stud_ref"))
                        {
                            if (cRow.Table.Columns[j].DataType.Name.Equals("Decimal") ||
                                cRow.Table.Columns[j].DataType.Name.Equals("Int32") ||
                                cRow.Table.Columns[j].DataType.Name.Equals("Int16") ||
                                cRow.Table.Columns[j].DataType.Name.Equals("SByte")
                                )
                            {
                                sw.Write(",{0}:{1}", cRow.Table.Columns[j].ColumnName, cRow[j]);
                            }
                            else
                            {
                                sw.Write(",{0}:\"{1}\"", cRow.Table.Columns[j].ColumnName, MK_Utils.str2html(cRow[j].ToString()));
                            }
                        }
                    }
                }
                sw.WriteLine(",\"marks\":[");
                int rid = 0;
                foreach (DataRow cRow in tb.Rows[i].GetChildRows("sr_mk"))
                {
                    if (rid > 0)
                    {
                        sw.WriteLine(",");
                    }
                    sw.WriteLine("{");
                    for (int j = 0; j < cRow.Table.Columns.Count; j++)
                    {
                        if (j > 0)
                        {
                            sw.WriteLine(",");
                        }
                        if (cRow.Table.Columns[j].DataType.Name.Equals("Decimal"))
                        {
                            sw.Write("{0}:{1}", cRow.Table.Columns[j].ColumnName, cRow[j]);
                        }
                        else
                        {
                            sw.Write("{0}:\"{1}\"", cRow.Table.Columns[j].ColumnName, cRow[j]);
                        }
                    }
                    sw.WriteLine("}");
                    rid++;
                }
                sw.WriteLine("]");
                sw.WriteLine(",\"acmarks\":[");
                rid = 0;
                foreach (DataRow cRow in tb.Rows[i].GetChildRows("sr_ac"))
                {
                    if (rid > 0)
                    {
                        sw.WriteLine(",");
                    }
                    sw.WriteLine("{");
                    for (int j = 0; j < cRow.Table.Columns.Count; j++)
                    {
                        if (j > 0)
                        {
                            sw.WriteLine(",");
                        }
                        sw.Write("{0}:\"{1}\"", cRow.Table.Columns[j].ColumnName, MK_Utils.str2html(cRow[j].ToString()));
                    }
                    sw.WriteLine("}");
                    rid++;
                }
                sw.WriteLine("]");
                sw.WriteLine(",\"gcmarks\":[");
                rid = 0;
                foreach (DataRow cRow in tb.Rows[i].GetChildRows("sr_gc"))
                {
                    if (rid > 0)
                    {
                        sw.WriteLine(",");
                    }
                    sw.WriteLine("{");
                    for (int j = 0; j < cRow.Table.Columns.Count; j++)
                    {
                        if (j > 0)
                        {
                            sw.WriteLine(",");
                        }
                        sw.Write("{0}:\"{1}\"", cRow.Table.Columns[j].ColumnName, MK_Utils.str2html(cRow[j].ToString()));
                    }
                    sw.WriteLine("}");
                    rid++;
                }
                sw.WriteLine("]");

                sw.WriteLine("}");
            }
            sw.WriteLine("]");
        }