环境: 我有数据存储在SQL2000数据库中,我使用VWD来维护我们公司的网站。 在过去,我使用asp.net + SQL数据库作为各个部门的接口,直到我遇到Json.NET。
业务流程: 这是我想要实现的。我在SQL数据库中有几个表,我可以根据用户的选择或在我的aspx网页中输入来查询项目任务,分配。我想将数据集序列化为JSON以进行进一步处理(总之,它可以在窗体中以甘特图的形式显示)。
以下是JSON示例:
{"tasks":[
{"id":-1,"name":"Gantt editor","code":"","level":0,"status":"STATUS_ACTIVE","start":1346623200000,"duration":16,"end":1348523999999,"startIsMilestone":true,"endIsMilestone":false,"assigs":[]},
{"id":-2,"name":"coding","code":"","level":1,"status":"STATUS_ACTIVE","start":1346623200000,"duration":10,"end":1347659999999,"startIsMilestone":false,"endIsMilestone":false,"assigs":[],"description":"","progress":0},
],
"selectedRow":0,
"deletedTaskIds":[],
"canWrite":true,
"canWriteOnParent":true }
为了做到这一点,我首先基于上面的JSON格式构造类。以下是我写的内容:
public class ganttData
{
public class Tasks
{
public int id { get; set; }
public string name { get; set; }
public string code { get; set; }
public int level { get; set; }
public string status { get; set; }
public object start { get; set; }
public int duration { get; set; }
public object end { get; set; }
public bool startIsMilestone { get; set; }
public bool endIsMilestone { get; set; }
public List<object> assigs { get; set; }
public string description { get; set; }
public int? progress { get; set; }
public string depends { get; set; }
}
public int selectedRow { get; set; }
public List<object> deletedTaskIds { get; set; }
public bool canWrite { get; set; }
public bool canWriteOnParent { get; set; }
}
我想构建类的原因是我想首先进行测试,看看程序是否可行。 但似乎它没有成功。
当然,掌握有更好的方法可以做到这一点,但这是我经过多次失败尝试后能够解决的最好方法。
现状: 到目前为止,我可以成功检索SQL表数据作为数据集,但我不知道如何将检索到的数据序列化为JSON。 数百万的感谢您的帮助。
舒曼
以下是我的***。aspx.cs文件中的代码。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;
using System.IO;
using System.Configuration;
public partial class Test_JNetToFile : System.Web.UI.Page
{
public class ganttData
{
public class Tasks
{
public int id { get; set; }
public string name { get; set; }
public string code { get; set; }
public int level { get; set; }
public string status { get; set; }
public object start { get; set; }
public int duration { get; set; }
public object end { get; set; }
public bool startIsMilestone { get; set; }
public bool endIsMilestone { get; set; }
public List<object> assigs { get; set; }
public string description { get; set; }
public int? progress { get; set; }
public string depends { get; set; }
}
//public List<Task> tasks { get; set; }
public int selectedRow { get; set; }
public List<object> deletedTaskIds { get; set; }
public bool canWrite { get; set; }
public bool canWriteOnParent { get; set; }
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
////get the connection string from web.config
////uncomment the following if you need to access sql database
////---------------------------------- Database connection----------------------------------------------
//string connString = ConfigurationManager.ConnectionStrings["ShipmentConnectionString"].ConnectionString;
//DataSet dataSet = new DataSet();
//using (SqlConnection conn = new SqlConnection(connString))
//{
// SqlDataAdapter adapter = new SqlDataAdapter();
// adapter.SelectCommand = new SqlCommand("select * from Gantt where Nr < 5", conn);
// conn.Open();
// adapter.Fill(dataSet);
//}
//string json = JsonConvert.SerializeObject(dataSet, Formatting.Indented);
////---------------------------------- Database connection----------------------------------------------
ganttData gd = new ganttData
{
Tasks =
{
{id = -1,name = "Gantt editor","code":"","level":0,"status":"STATUS_ACTIVE","start":1346623200000,"duration":16,"end":1348523999999,"startIsMilestone":true,"endIsMilestone":false,"assigs":[]},
{id = -2,name = "coding","code":"","level":1,"status":"STATUS_ACTIVE","start":1346623200000,"duration":10,"end":1347659999999,"startIsMilestone":false,"endIsMilestone":false,"assigs":[],"description":"","progress":0},
{id = -3,name = "gant part","code":"","level":2,"status":"STATUS_ACTIVE","start":1346623200000,"duration":2,"end":1346795999999,"startIsMilestone":false,"endIsMilestone":false,"assigs":[],"depends":""},
{id = -4,name = "editor part","code":"","level":2,"status":"STATUS_SUSPENDED","start":1346796000000,"duration":4,"end":1347314399999,"startIsMilestone":false,"endIsMilestone":false,"assigs":[],"depends":"3"},
{id = -5,name = "testing","code":"","level":1,"status":"STATUS_SUSPENDED","start":1347832800000,"duration":6,"end":1348523999999,"startIsMilestone":false,"endIsMilestone":false,"assigs":[],"depends":"2:5","description":"","progress":0},
{id = -6,name = "test on safari","code":"","level":2,"status":"STATUS_SUSPENDED","start":1347832800000,"duration":2,"end":1348005599999,"startIsMilestone":false,"endIsMilestone":false,"assigs":[],"depends":""},
{id = -7,name = "test on ie","code":"","level":2,"status":"STATUS_SUSPENDED","start":1348005600000,"duration":3,"end":1348264799999,"startIsMilestone":false,"endIsMilestone":false,"assigs":[],"depends":"6"},
{id = -8,name = "test on chrome","code":"","level":2,"status":"STATUS_SUSPENDED","start":1348005600000,"duration":2,"end":1348178399999,"startIsMilestone":false,"endIsMilestone":false,"assigs":[],"depends":"6"}
},
selectedRow = 0,
deletedTaskIds = [],
canWrite = true,
canWriteOnParent = true
};
// serialize JSON to a string and then write string to a file
File.WriteAllText(@"c:\movie.json", JsonConvert.SerializeObject(movie));
// serialize JSON directly to a file
using (StreamWriter file = File.CreateText(@"c:\movie.json"))
{
JsonSerializer serializer = new JsonSerializer();
serializer.Serialize(file, movie);
}
//Label1.Text = json;
}
}