C# 使用mysql数据的C创建Json文件

C# 使用mysql数据的C创建Json文件,c#,mysql,json,C#,Mysql,Json,我试图创建一个mysql数据库的json文件,它与下面的代码一起工作,但我不知道如何给他们一个标题,我需要在android studio中截取这个json的数据,我需要: **这只是一个例子,让你知道我需要哪个部分,我不需要颜色** { **"colors"**: [// i need this section here but with my code i couldnt //get this. { "color": "black", "

我试图创建一个mysql数据库的json文件,它与下面的代码一起工作,但我不知道如何给他们一个标题,我需要在android studio中截取这个json的数据,我需要: **这只是一个例子,让你知道我需要哪个部分,我不需要颜色**

 {
     **"colors"**: [//   i need this section here but with my code i couldnt 
   //get this.
    {
      "color": "black",
      "category": "hue",
      "type": "primary",
      "code": {
       "rgba": [255,255,255,1],
       "hex": "#000"
      }
 } 
android studio中的凌空截击是为了获取数据:

JSONArray JSONArray=response.getJSONArraycolors

下面是我用C编写的在本地主机上创建Json文件的代码

namespace MySqlServerDemo
{
    public partial class WebForm1 : System.Web.UI.Page
    {


        protected void Page_Load(object sender, EventArgs e)
        {

            Response.Write( ListJson() );

        }

        //    [WebMethod]
        public static string ListJson()
        {
            DataSet ds = new DataSet();

            MySqlConnection con = new MySqlConnection("server=localhost;user id=root;database=studentdetails;password=MYPASSWORD");
            con.Open();
            MySqlCommand cmd = new MySqlCommand("select * from STUDENTS", con);
            MySqlDataAdapter da = new MySqlDataAdapter(cmd);


            da.Fill(ds);

                List<students> studentDetails = new List<students>();
                studentDetails = ConvertDataTable<students>(ds.Tables[0]);

            JavaScriptSerializer js = new JavaScriptSerializer();
                return js.Serialize(studentDetails);

            }

        public class students
        {
            public string firstname { get; set; }
            public string surname { get; set; }

        }


        private static List<T> ConvertDataTable<T>(DataTable dt)
        {
            List<T> data = new List<T>();
            foreach (DataRow row in dt.Rows)
            {
                T item = GetItem<T>(row);
                data.Add(item);
            }
            return data;
        }
        private static T GetItem<T>(DataRow dr)
        {
            Type temp = typeof(T);
            T obj = Activator.CreateInstance<T>();

            foreach (DataColumn column in dr.Table.Columns)
            {
                foreach (PropertyInfo pro in temp.GetProperties())
                {
                    if (pro.Name == column.ColumnName)
                        pro.SetValue(obj, dr[column.ColumnName], null);
                    else
                        continue;
                }
            }
            return obj;
        }
    }
}

您可以这样简单地编写一个包装器类

公共类集装箱类{ 公共列表学生{get;set;} } 并将其序列化

因此,在ListJson方法中最终要做的是:

var studentDetails=新的集装箱类; studentDetails.Students=convertdatatables.Tables[0]; JavaScriptSerializer js=新的JavaScriptSerializer; 返回js.studentdetails;
这有意义吗?

你能告诉我你在哪里转换颜色类别吗?您显示的代码加载了一个Students类,它只是一个示例,让您知道我需要哪个部分,因此对于我的部分是这样的:student:[{firstname:jacob姓氏:hello}]您必须使您的类可序列化。例如,您的类student包含一个对象StudentInfo,该对象具有您想要显示的所有属性,然后使用类声明上方的标题[serializable]使这两个对象都可序列化。您是否可以编写此操作?我不确定我应该在何处编写serializable此操作有效谢谢!但我无法在android中获取数据,但这将是另一个问题的主题。