如何编辑我的代码以json格式返回此Web服务方法。 [英] How do I edit my code to return this webservice method in json format.
问题描述
我是所有这些的新手,我想从我的小部件中实际使用这个webservice方法来显示事件。我的小部件以JSON格式使用,我听说我需要确保Web服务mehod中返回的值是JSON格式。所以我想知道哪些部分需要改变。
非常感谢您提前。
[WebMethod(EnableSession = true ,Description = 典型的Web方法)]
public List< string> retrieveEvents()
{
List< string> retrieveData = new List< string>();
尝试
{
使用(SPSite oSPSite = new SPSite( http:.....))
{
SPList list = oSPWeb.Lists.TryGetList( Calendar跨度>);
SPQuery query = new SPQuery();
query.Query = < Where>< And>< Geq>< ; FieldRef Name ='EventDate'/>< Value Type ='DateTime'> + SPUtility.CreateISO8601DateTimeFromSystemDateTime(DateTime.Today)+ < / Value>< / Geq> +< Leq>< FieldRef Name ='EndDate'/>< Value Type ='DateTime'> + SPUtility.CreateISO8601DateTimeFromSystemDateTime(DateTime.Today)+ < / Value>< / Leq>< / And> ;< /何处>中跨度>;
// query.ExpandRecurrence = true;
query.ViewFields = @ < FieldRef Name ='Title'/>< ; FieldRef Name ='EventDate'/>< FieldRef Name ='EndDate'/>< FieldRef Name ='Location'/>;
SPListItemCollection items = list.GetItems(query);
foreach (SPListItem listItem in items)
{
retrieveData。添加(listItem [ Title]。ToString());
retrieveData.Add(listItem [ EventDate]。ToString());
retrieveData.Add(listItem [ EndDate]。ToString());
retrieveData.Add(listItem [ Location]。ToString());
}
return retrieveData;
}
}
}
catch (例外情况)
{
}
return retrieveData;
}
要获得纯json格式,您可以使用如下所示的javascript序列化程序。
public class WebService1:System.Web.Services.WebService
{
[WebMethod]
[ ScriptMethod(UseHttpGet = true,ResponseFormat = ResponseFormat.Json)]
public void HelloWorld()
{
JavaScriptSerializer js = new JavaScriptSerializer();
Context.Response.Clear();
Context.Response.ContentType =application / json;
Context.Response.Write(js.Serialize(retrieveData));
}
}
是的,你需要设置javascript端代码dataType:json
.ajax({
type:POST,
contentType:application / json; charset = utf-8,
url:YourWebServiceName.asmx / HelloWorld ,
数据:{''},
dataType:json,
成功:函数(msg){
OnSucessCallBack(data);
},
错误:函数(xhr,状态,错误){
alert(xhr.statusText);
}
});
我相信这样做:
// 您需要引用System.Web.Extensions
使用 System.Web.Script.Serialization;
var jsonSerialiser = new JavaScriptSerializer();
var json = jsonSerialiser.Serialize(aList);
取自 http://stackoverflow.com/questions/9110724/serializing-a-list-to -json [ ^
Hi,
I'm new to all these, I want to actually consume this webservice method from my widget to display out the events. My widget is consuming in JSON format, and I've heard that I need to ensure that the value returned in the web service mehod is in JSON format. So I would like to know which are the parts that I need to change.
Thank you very very much in advance.
[WebMethod(EnableSession = true, Description = "Typical Web Method")]
public List<string> retrieveEvents()
{
List<string> retrievedData = new List<string>();
try
{
using (SPSite oSPSite = new SPSite("http:....."))
{
SPList list = oSPWeb.Lists.TryGetList("Calendar");
SPQuery query = new SPQuery();
query.Query = "<Where><And><Geq><FieldRef Name='EventDate' /><Value Type='DateTime'>" + SPUtility.CreateISO8601DateTimeFromSystemDateTime(DateTime.Today) + "</Value></Geq> + <Leq><FieldRef Name='EndDate' /><Value Type='DateTime'>" + SPUtility.CreateISO8601DateTimeFromSystemDateTime(DateTime.Today) + "</Value></Leq></And></Where>";
// query.ExpandRecurrence = true;
query.ViewFields = @"<FieldRef Name='Title' /><FieldRef Name='EventDate' /><FieldRef Name='EndDate' /><FieldRef Name='Location' />";
SPListItemCollection items = list.GetItems(query);
foreach (SPListItem listItem in items)
{
retrievedData.Add(listItem["Title"].ToString());
retrievedData.Add(listItem["EventDate"].ToString());
retrievedData.Add(listItem["EndDate"].ToString());
retrievedData.Add(listItem["Location"].ToString());
}
return retrievedData;
}
}
}
catch (Exception ex)
{
}
return retrievedData;
}
To get a pure json format, you can use javascript serializer like below.
public class WebService1 : System.Web.Services.WebService { [WebMethod] [ScriptMethod(UseHttpGet=true ,ResponseFormat = ResponseFormat.Json)] public void HelloWorld() { JavaScriptSerializer js = new JavaScriptSerializer(); Context.Response.Clear(); Context.Response.ContentType = "application/json"; Context.Response.Write(js.Serialize(retrievedData)); } }
Yes at javascript side code you need to setdataType: "json"
.ajax({ type: "POST", contentType: "application/json; charset=utf-8", url: "YourWebServiceName.asmx/HelloWorld", data: "{''}", dataType: "json", success: function (msg) { OnSucessCallBack(data); }, error: function (xhr, status, error) { alert(xhr.statusText); } });
I believe this will do it:
// you need to reference System.Web.Extensions using System.Web.Script.Serialization; var jsonSerialiser = new JavaScriptSerializer(); var json = jsonSerialiser.Serialize(aList);
Taken from http://stackoverflow.com/questions/9110724/serializing-a-list-to-json[^]
这篇关于如何编辑我的代码以json格式返回此Web服务方法。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!