用Jsonp输出数据

前端之家收集整理的这篇文章主要介绍了用Jsonp输出数据前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

页面输出的格式如下(3条数据):

json1246574([{"site_Id":"E001","group_Id":"11","id":"4","parentId":"1","sm_Career":"4","sm_CountryID":"3","sm_Level":"120","sm_PlayerName":"TJ春哥","sm_UID":"11259595285420516","sm_UpState":"4","sm_UpdateTime":"1264236590","sm_officeRankBase":"91"}],

[{"site_Id":"E001","id":"3","sm_Career":"1","sm_PlayerName":"TJ卡耐基","sm_UID":"11001897265572627","sm_UpState":"5","sm_UpdateTime":"1264236028","sm_officeRankBase":"95"}],"id":"2","sm_Career":"3","sm_CountryID":"2","sm_PlayerName":"天边飞羽","sm_UID":"11001897304015173","sm_UpState":"3","sm_UpdateTime":"1264179528","sm_officeRankBase":"36"}])

===================================================================

//Json获取数据的函数,其中rankName、rankType通过url赋值

public static string GetRankInfoForWeb(int count,string siteId,string groupId,string rankName,string rankType)
{
StringBuilder builder = null;

try
{
DataTable dt =
AllService.GetDataFromRankNameByTopNRank(count,siteId,groupId,rankName,rankType).Tables[0];

if (dt != null)
{
builder = new StringBuilder();
// builder.Append("({");
builder.Append("(");
if (rankName == "Army_ArmyPower") //军团——强度
{
for (int i = 0; i < dt.Rows.Count; i++)
{
builder.Append("[{");
builder.AppendFormat(
"/"site_Id/":/"{0}/",/"group_Id/":/"{1}/",/"id/":/"{2}/",/"parentId/":/"{3}/",/"sm_ArmyLeaderLevel/":/"{4}/","
+
"/"sm_ArmyLevel/":/"{5}/",/"sm_ArmyName/":/"{6}/",/"sm_ArmyPeople/":/"{7}/",/"sm_ArmyPower/":/"{8}/","
+ "/"sm_CountryID/":/"{9}/",/"sm_UpState/":/"{10}/",/"sm_ArmyID/":/"{11}/"",
dt.Rows[i]["site_id"],dt.Rows[i]["group_id"],dt.Rows[i]["id"],dt.Rows[i]["parentID"],
dt.Rows[i]["sm_ArmyLeaderLevel"],dt.Rows[i]["sm_ArmyLevel"],dt.Rows[i]["sm_ArmyName"],
dt.Rows[i]["sm_ArmyPeople"],dt.Rows[i]["sm_ArmyPower"],dt.Rows[i]["sm_CountryID"],
dt.Rows[i]["sm_UpState"],dt.Rows[i]["sm_ArmyID"]); //dt.Rows[0]["rowtime"],
builder.Append("}]");
builder.Append(",");
}
}
if (rankName == "Level_LevelAll") //等级——个人
{
for (int i = 0; i < dt.Rows.Count;i++ )
{
builder.Append("[{");
builder.AppendFormat(
"/"site_Id/":/"{0}/",/"sm_Career/":/"{4}/","
+
"/"sm_CountryID/":/"{5}/",/"sm_Level/":/"{6}/",/"sm_PlayerName/":/"{7}/",/"sm_UID/":/"{8}/","
+ "/"sm_UpState/":/"{9}/",/"sm_UpdateTime/":/"{10}/",/"sm_officeRankBase/":/"{11}/"",
dt.Rows[i]["sm_Career"],dt.Rows[i]["sm_Level"],
dt.Rows[i]["sm_PlayerName"],dt.Rows[i]["sm_UID"],dt.Rows[i]["sm_UpState"],
dt.Rows[i]["sm_UpdateTime"],dt.Rows[i]["sm_officeRankBase"]);
builder.Append("}]");
builder.Append(",");
}
}
builder.Append("}");
//builder.Append(",");
builder.Remove(builder.Length - 1,1);
builder.Append(")");
builder.Remove(builder.Length - 2,1);//去掉倒数第二个符号,
}
}
catch (Exception ex)
{
LogHelper.Error("获取排行榜中信息异常,方法名[GetRankInfoForWeb()]",ex);
}
string nullStr = "信息为空";
string sbStr = string.Empty;
if (builder != null)
{
sbStr = Equals(builder,nullStr) ? nullStr : builder.ToString();
return sbStr;
}
return sbStr;
}

//页面调用,callback是通过url赋值

if(!IsPostBack)
{
try
{
string callback = Request.QueryString["callback"].ToString();
// int count = int.Parse(ConfigurationManager.AppSettings["TopNforweb"].ToString());
int count = 3;
string rankName = ""; //排行榜名称
string rankType = ""; //排行的类型
string str = "";
rankName = Request.QueryString["rankName"].ToString();
rankType = Request.QueryString["rankType"].ToString();
if (!string.IsNullOrEmpty(rankType))
{
str = OutPutService.GetRankInfoForWeb(count,"",rankType); //军团—强度;等级——个人
}
else
{
Response.Write("请输入正确的参数!");
}

Response.Write(callback + str);
}
catch (Exception ex)
{
Response.Write("提示:请输入正确的参数。");
}
}

其他备注:

<form id="form1" runat="server" method="post" action="OutPutDataWrite.aspx?rankName=?&&rankType=?&&callback=?">

OutPutDataForWeb.aspx?rankName=Level_LevelAll&&rankType=sm_Level&&callback=json1246574

原文链接:https://www.f2er.com/json/290825.html

猜你在找的Json相关文章