js读取json
Json字符串是:
[{"n":"aaa","un":"aaa"},{"n":"yang","un":"yang"},{"n":"wang","un":"wang"},{"n":"jone","un":"jone"},{"n":"fwry001","un":"fwry001"}]
/// <summary>
/// 2013-11-18
/// </summary>
/// <returns></returns>
public object[] GetBS()
{
string sql = "select SS_NAME,SS_REALNAME from B_SERVICE_STAFF";
DataTable dt = DbAccessor.Query(sql).Tables[0];
int count=dt.Rows.Count;
object[] o=new object[count];
for (int i = 0; i < count;i++ )
{
o[i] = new { sn = dt.Rows[i]["SS_NAME"].ToString(), un = dt.Rows[i]["SS_REAlNAME"].ToString() };
}
return o;
}
/// <summary>
/// jcz 获取服务人员
/// </summary>
/// <returns></returns>
public JsonResult GetBS()
{
object[] o = bsStaff.GetBS();
return Json(o,JsonRequestBehavior.AllowGet);
}
<script type="text/javascript" language="javascript">
$(function () {
$.ajax({
type: 'post', //data:’Json’这句不能写,不然报错
url: '@Url.Content("~/Home/GetBS")',
success: function (data) {
var listr = "";
for (var o in data) {
listr += "<p> <img src=" + '@Url.Content("~/Contents/Common/images/photo.png")' + "/>" + data[o].un + "</p>";
}
$("#staffli").html(listr);
},
error: function (err) {
debugger;
alert('发送系统错误,请与系统管理员联系!');
},
beforeSend: function () {
},
complete: function () {
}
});
})
</script>
-----------------------------------以上的json内部是数组--------------------------------
[数字1,数字2]
public JsonResult GetCount()
{
object[] o=new object[2];
int cTask = t.GetTaskCount();
int cApply= p.GetApplyCount();
o[0] = cTask;
o[1] = cApply;
return Json(o, JsonRequestBehavior.AllowGet);
}
<script type="text/javascript" language="javascript">
$(function () {
SetCount2();
})
function SetCount2(){
SetCount();
setTimeout(function () {
SetCount2();
}, @ViewBag.CountTimeUpd);
}
function SetCount(){
$.ajax({
type: 'post',
url: '@Url.Content("~/Home/GetCount")',
data:'json',
success: function (mes) {
$("#cT").html("("+mes[0]+")");
$("#cA").html("("+mes[1]+")");
},
error: function () {
Dialog.Alert('发送系统错误,请与系统管理员联系!');
},
beforeSend: function () {
},
complete: function () {
}
});
}
</script>
----------------------------------以上json内部就是数字-------------------------------