我正在寻找一种将数据导出为JSON格式的方法,请指导我。
最后,我想返回数据的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代码](https://istack.dev59.com/mcWk8.webp)
请帮我解决这个问题!谢谢