我正在尝试创建一个调用sharepoint rest查询的本地代理。
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static string getJson()
{
string listRSSFeedUrl = "http://myurl/_vti_bin/listdata.svc/MyList";
var req = (HttpWebRequest)WebRequest.Create(listRSSFeedUrl);
req.Credentials = CredentialCache.DefaultNetworkCredentials;
req.PreAuthenticate = true;
var res = (HttpWebResponse)req.GetResponse();
var feed = new XmlDocument();
var feedStream = res.GetResponseStream();
feed.Load(feedStream);
return feed.InnerXml;
}
如何返回原始rest查询应返回的json格式?
调用该方法的jquery如下所示:
$.ajax({
type: "POST",
url: "SharepointJson.aspx/getJson",
cache: false,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
var markup = "Announcements:<br /><br />";
$.each(data.d.results, function (i, result) {
//Display some properties
markup += 'Title: ' + result.feed + '<br />';
});
//Call the jQuery append method to display the HTML
$('#displayDiv').append($(markup));
}
});
答案 0 :(得分:0)
只是猜测,但您可以尝试使用JavaScriptSerializer:
using (var response = (HttpWebResponse)request.GetResponse())
{
using (var reader = new StreamReader(response.GetResponseStream()))
{
JavaScriptSerializer js = new JavaScriptSerializer();
var obj = js.Deserialize<dynamic>(reader.ReadToEnd());
}
}
并返回反序列化的对象。