【发布时间】:2016-12-15 01:17:43
【问题描述】:
我使用 VS2015 在 WCF WebServices 中创建了以下几组方法
[ServiceContract]
public interface ISchoolProjectService
{
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
RequestFormat =WebMessageFormat.Json,
UriTemplate = "field=news/{id}")]
IList<Object> NewsService(string id);
}
在它的实现中
public IList<Object> NewsService(string id)
{
try
{
_entitites = new SchoolEntities();
var query = from x in _entitites.NewsAnnouncements select x;
switch(id)
{
case "all":
return query.ToList<Object>();
break;
default:
return null;
break;
}
}catch(Exception e)
{
return null;
}
}
浏览器中的服务请求 /field=news/all 我收到以下回复
{"NewsServiceResult":"Your requested product[{\"Id\":\"02ed1de9-4029-4b94-869d-4be55e82edc8\",\"Title\":\"Relief, happiness and disappointment as VCE results released\",\"Image\":\"47c05ca9-d126-4823-8e16-17d499c78b5d.jpg\",\"Description\":\"For five excruciating days, Natasha Kennedy resisted the temptation to open&nbsp;her VCE results.She was one of more than 2000 students who received their results early due to a&nbsp;technical glitch. At first she thought it was a cruel hoax, and then she was prepared to wait.\",\"PublishDate\":\"2016-04-13T00:00:00\",\"CreatedDate\":\"2016-04-06T10:37:30\",\"UserName\":\"WebAdmin\",\"ShortDescription\":\"For five excruciating days, Natasha Kennedy resisted the temptation to open her VCE results.\"}
我想知道有没有机会像这样格式化这些数据
{
"field": "news",
"sortBy": "all",
"articles": [
{
"id": "02ed1de9-4029-4b94-869d-4be55e82edc8",
"title": "Relief, happiness and disappointment as VCE results released",
"shortDescription": "For five excruciating days, Natasha Kennedy resisted the temptation to open her VCE results.",
"urlToDescription": "http://webapischoolproject.yarshatech.com/Detail/NewsAndAnnouncement/02ed1de9-4029-4b94-869d-4be55e82edc8",
"urlToImage": "http://http://webapischoolproject.yarshatech.com/Detail/NewsAndAnnouncement/47c05ca9-d126-4823-8e16-17d499c78b5d.jpg",
"publishDate": "2016-12-14T23:37:03Z",
"createDate":"2016-12-14T23:37:03Z"
},
我真正想要从 json 响应中将具有长文本的描述字段格式化为
"urlToDescription":"http://webapischoolproject.yarshatech.com/Detail/NewsAndAnnouncement/02ed1de9-4029-4b94-869d-4be55e82edc8"
和图像成
"urlToImage":"http://webapischoolproject.yarshatech.com/Detail/NewsAndAnnouncement/47c05ca9-d126-4823-8e16-17d499c78b5d.jpg"
【问题讨论】:
-
如果您在服务的端点行为中添加
您应该得到没有包装器的 json。查看此博客:blog.clauskonrad.net/2010/11/…
标签: c# json entity-framework wcf