使用C#的mysql数据创建Json文件 [英] Creating Json file with C# of mysql data

查看:303
本文介绍了使用C#的mysql数据创建Json文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个mysql数据库的json文件,并且它与下面的代码一起工作,但是我不知道如何给他们一个标头",我需要在我需要的android studio中使用volley从这个json中获取数据: **这只是一个例子,可以让您知道我需要哪一部分,而我不需要颜色" **

I tried to create a json file of mysql database and it works with the code down below but i dont know how to give them a "header" that i need to fetch data from this json with volley in android studio i need: **this is only an example to let you know which section i need , i dont need "colors" **

 {
     **"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中的VOLLEY这样做是为了获取数据:

VOLLEY in android studio does this to get data :

JSONArray jsonArray = response.getJSONArray("颜色");

在C#中使用我的代码在本地主机上创建Json文件

Heres my code in C# for creating a Json file at localhost

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;
        }
    }
}

推荐答案

您可以像这样简单地编写包装器类.

You can simply write a wrapper class like so.

public class ContainerClass {
    public List<students> Students {get;set;}
}

并对其进行序列化.

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

So what you would end up doing in your ListJson method is:

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

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

这有意义吗?

这篇关于使用C#的mysql数据创建Json文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆