.net后台将查询的字符串转换为json并且输出到前端显示

.net前端部分


    

.net后端部分

public partial class _default : System.Web.UI.Page
{

    public class User
    {
        public User(string name, string phone)
        {
            username = name;
            tel = phone;
        }

        public string username { get; set; }
        public string tel { get; set; }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        string action = Request.QueryString.Get("action");
        if (action == "getusers")
        {
            string input = "username1, pwd1, 1311111111; username2, pwd2, 1311111111; username3, pwd3, 1311111111";
            string[] user_infos = input.Trim().Split(';');
            List user_list = new List();

            foreach (string userinfo in user_infos)
            {
                string[] info_metas = userinfo.Split(',');
                string username = info_metas[0];
                string tel = info_metas[2];

                User user = new User(username, tel);
                user_list.Add(user);
            }

            if (user_list.Count > 0)
            {
                var rsp_obj = new
                {
                    code = 200,
                    user = user_list
                };

                Response.Write(JsonConvert.SerializeObject(rsp_obj));//将rsp_obj转化为json并输出
                Response.End();
            }
        }
    }

你可能感兴趣的:(.net后台将查询的字符串转换为json并且输出到前端显示)