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

3
我正在寻找一种将数据导出为JSON格式的方法,请指导我。
<%@ WebHandler Language="C#" Class="API" %>

using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using Newtonsoft.Json;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using System.Web.Script.Serialization;
public class User
{
    public string type { get; set; }
    public string user { get; set; }
    public string pass { get; set; }

}
public class API : IHttpHandler
{

public void ProcessRequest(HttpContext context)
{
    context.Response.ContentType = "text/plain";
    string strJson = new StreamReader(context.Request.InputStream).ReadToEnd();
    User user = JsonConvert.DeserializeObject<User>(strJson);
    string str = System.Configuration.ConfigurationManager.ConnectionStrings["conStr"].ConnectionString, json = "";
    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();
        DataTable datatable = new DataTable();
        SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(cmd);
        con.Close();
        sqlDataAdapter.Fill(datatable);
        if (datatable.Rows.Count > 0)
        {
            foreach (DataRow dr in datatable.Rows)
                //My data return;
        }
    }
}

最后,我想返回数据的json格式
我的HTML/Javascript代码

请帮我解决这个问题!谢谢


您已将内容类型设置为text/plain。如果您希望浏览器将数据解释为JSON,则应适当设置MIME类型 -“application/json”。为了序列化您的数据,建议使用库,因为任何小错误都可能损坏文件。总的来说,似乎最好使用某种Web服务来实现您想要的目标。 - soupy-norman
用文本代码替换那张代码图片。 - barbsan
2个回答

2

谢谢您的评论,我会尝试的。 - Please Help Me

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接