我想从c#webmethod返回一个JSON对象,如果我使用return我有一个错误“找到了recussive reference”。所以我决定使用newtonsoft序列化,但返回的对象是双引号。 {d:“我的json”}并且没有用。
我尝试使用此代码:
[WebMethod(EnableSession = true)]
public static void RecordList(int jtStartIndex = 0, int jtPageSize = 0, string jtSorting = null)
{
string error;
var result = DataTools.Data("select id_individu,nom_indiv from individu limit 10",
WebConfigurationManager.ConnectionStrings["TabloidConnection"].ConnectionString,
out error);
var rep = System.Web.HttpContext.Current.Response;
rep.Clear();
rep.ClearContent();
rep.BufferOutput = true;
rep.ContentType = "application/json; charset=UTF-8";
//rep.Write("ab");
rep.Write("{d:");
rep.Write(JsonConvert.SerializeObject(new jTableResponse(result), Formatting.None));
rep.Write("}");
rep.Flush();
//rep.SuppressContent = true;
rep.End();
//ct.ApplicationInstance.CompleteRequest();
}
public class jTableResponse
{
private int totalRecordCount=0;
private DataTable records;
private string result = "OK";
public string Result
{
get { return result; }
set { result = value; }
}
public int TotalRecordCount
{
get { return totalRecordCount; }
set { totalRecordCount = value; }
}
public DataTable Records
{
get { return records; }
set { records = value; }
}
public jTableResponse(DataTable r)
{
Records = r;
totalRecordCount = r.Rows.Count;
}
}
但我在我的json之后添加了角色(使用fiddler)
1D0 {d:{ “结果”: “OK”, “TotalRecordCount”:10, “记录”:[{ “id_individu”:1, “nom_indiv”:” ... “},{” id_individu “:2”,nom_indiv “:” 铆钉 “},{” id_individu “:3”,nom_indiv “:” 科恩 “},{” id_individu “:4”,nom_indiv “:” VILLEROUGE “},{” id_individu “:5”,nom_indiv“: “CAILLARD”},{“id_individu”:6,“nom_indiv”:“DE CHEVRON VILLETTE”},{“id_individu”:7,“nom_indiv”:“ROYO”},{“id_individu”:8,“nom_indiv”: “ROQUES”},{ “id_individu”:9 “nom_indiv”: “GUILLOT”},{ “id_individu”:10 “nom_indiv”: “多恩-CORRAZE”}]}} 0
感谢您的回复
答案 0 :(得分:0)
当我改变这个
rep.Write("{d:");
rep.Write(JsonConvert.SerializeObject(new jTableResponse(result), Formatting.None));
rep.Write("}");
由此
rep.Write(JsonConvert.SerializeObject(new jTableResponse(result), Formatting.None));
工作得很好。