如何在ashx中返回数据类型JSON

时间:2018-11-29 08:47:26

标签: c# asp.net asp.net-ajax generic-handler

我正在寻找一种将数据导出为JSON样式的方法。请给我指路

release {               
            minifyEnabled false
        }

最后我想返回数据json
My Code HTML/Javascript

请帮助我解决问题!谢谢

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

if (user != null)
    {
        if (user.type != null && user.user != null && user.pass != null)
        {
            SqlConnection con = new SqlConnection(str);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = "SELECT ID,UserName From Partner ";
            //Where UserName=@UserName And PassWord=@Pass
            //cmd.Parameters.Add("@UserName", SqlDbType.VarChar, 100).Value = user.user;
            //  cmd.Parameters.Add("@Pass", SqlDbType.NVarChar, 100).Value = user.pass;
            if (ConnectionState.Closed == con.State)
                con.Open();
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(cmd);
            con.Close();
            sqlDataAdapter.Fill(datatable);
            int i = 0;
            if (datatable.Rows.Count > 0)
            {
                foreach (DataRow dr in datatable.Rows)
                {
                    json += "A" + i + ":{ID:'" + dr["ID"] + "',User:'" + dr["UserName"] + "'},";
                    i++;
                }
                json = json.Remove(json.Length - 1);
                json += "}";
                JObject json2 = JObject.Parse(json);
                context.Response.Write(json2);
                return;
            }
            else
                json = "{'result':'false'}";
        }
        else
            json = "{'result':'false'}";
        JObject json3 = JObject.Parse(json);
        context.Response.Write(json3);
        return;
    }