如何在web api中获取整个数组的头或键名称。尝试了几种选择。无法解决 [英] how to get header or key name for entire array in web api. tried several option. not able to solve
问题描述
CusvalController.cs
*********************
// GET api / cusval
string constring = string.Empty;
// public DataTable Get()
public DataTable Get()
{
constring = ConfigurationManager.ConnectionStrings [sqlconnectstring]。ConnectionString.ToString();
DataTable dt = new DataTable();
NameValueCollection nvc = HttpUtility.ParseQueryString(Request.RequestUri.Query);
var u = nvc [uname];
var p = nvc [pwd];
var sqlstring = nvc [sql];
SqlCommand cmd = new SqlCommand(SELECT * FROM login where uname ='+ username +'and pwd ='+ pwd +',con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
}
返回dt;
}
所以,当我试图获得JSON响应时,我第一次得到错误要么。所以,我在下面的编码中使用了Register()...在global.ascx.cs然后它工作了
global.ascx.cs
****************
命名空间API
{
//注意:有关启用IIS6或IIS7经典模式的说明,
//访问http://go.microsoft.com/?LinkId=9394801 < br $>
公共类WebApiApplication:System.Web.HttpApplication
{
公共静态类WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
名称:DefaultApi,
routeTemplate:api / {controller},
默认值:new {id = RouteParameter.Optional}
);
var json = config.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
config.Formatters.Remove(config.Formatters.XmlFormatter); <无线电通信/>
}
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas( );
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
}
现在我在浏览器上得到了JSON respone,如下所示:
[< br $> b $ b
- {
uname:christhu,
地点:chennai,
状态:好
} < br $> b $ b
]
所以,问题是。我无法进行解析,因为它没有根密钥名称。只需要为它设置一个通用名称。我的意思是我需要得到像下面那样的老响应的响应。
[
- 结果:{
uname:christhu,
地点:chennai,
状态:好
}
]
$ b需要$ b帮助。
CusvalController.cs
*********************
// GET api/cusval
string constring = string.Empty;
// public DataTable Get()
public DataTable Get()
{
constring = ConfigurationManager.ConnectionStrings["sqlconnectstring"].ConnectionString.ToString();
DataTable dt = new DataTable();
NameValueCollection nvc = HttpUtility.ParseQueryString(Request.RequestUri.Query);
var u = nvc["uname"];
var p = nvc["pwd"];
var sqlstring = nvc["sql"];
SqlCommand cmd = new SqlCommand("SELECT * FROM login where uname='" + username + "' and pwd='" + pwd + "'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
}
return dt;
}
so, when i try to get JSON response, first time i got error. so, i used below coding in Register()... in global.ascx.cs then it worked
global.ascx.cs
****************
namespace API
{
// Note: For instructions on enabling IIS6 or IIS7 classic mode,
// visit http://go.microsoft.com/?LinkId=9394801
public class WebApiApplication : System.Web.HttpApplication
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}",
defaults: new { id = RouteParameter.Optional }
);
var json = config.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
config.Formatters.Remove(config.Formatters.XmlFormatter);
}
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
}
Now i got the JSON respone on browser like below:
[
- {
uname:"christhu",
place:"chennai",
status:"good"
}
]
so, the problem is. i am not able to do parsing as it does not have root key name. just need to put one common name for that.i mean i need to get response like below insteated of old respone.
[
- Result: {
uname:"christhu",
place:"chennai",
status:"good"
}
]
help needed.
推荐答案
这篇关于如何在web api中获取整个数组的头或键名称。尝试了几种选择。无法解决的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!