如何将Json对象读取为整数 [英] How do I get a Json Object to be read as an integer
本文介绍了如何将Json对象读取为整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不能让json1读取{id:1}表示整数而不是它读取{id:1}并且不能在[HttpPost]中读取整数。
如何将Json对象读取为整数?
I can not get the json1 to read { "id":1 } for an Integer instead it reads { "id":"1" } and can not be read as an integer in the [HttpPost].
How do I get a Json Object to be read as an integer?
script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$("#btn1").click(function () {
var id1 = $("#text1").val();
var url1 = "/Home/function1";
var data1 = { "id":id1 };
var json1 = JSON.stringify(data1);
$("#jsondata").val(json1);
$.post(url1, json1, function (data) {
alert("success");
},"json");
$("#id").val(data[0].Id);
$("#col1").val(data[0].col1);
$("#col2").val(data[0].col2);
$("#col3").val(data[0].col3);
});
</script>
[HttpPost]
public JsonResult function1(int id)
{
List<Table1> the1 = new List<Table1>();
string connStr = ConfigurationManager.ConnectionStrings["conndb"].ConnectionString;
string cmdStr = "SELECT * FROM [Table1] WHERE [Id]=@Id;";
SqlDataReader dr = null;
using (SqlConnection conn = new SqlConnection(connStr))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand(cmdStr, conn))
{
cmd.Parameters.AddWithValue("@Id", id);
cmd.ExecuteNonQuery();
using (dr = cmd.ExecuteReader())
{
while (dr.Read())
{
the1.Add(new Table1 { Id = Convert.ToInt32(dr["Id"]), col1 = dr["col1"].ToString(), col2 = dr["col2"].ToString(), col3 = dr["col3"].ToString() });
}
}
}
}
return Json(the1, JsonRequestBehavior.AllowGet);
}
推荐答案
( function (){
( #btn1)。click( function (){
var id1 =
("#btn1").click(function () { var id1 =
( #text1)。val();
var url1 = / Home / function1;
var data1 = { id:id1};
var json1 = JSON .stringify(data1);
("#text1").val(); var url1 = "/Home/function1"; var data1 = { "id":id1 }; var json1 = JSON.stringify(data1);
这篇关于如何将Json对象读取为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文