如何格式化标准json格式的WCF服务中EF实体返回的json数据?

时间:2016-12-15 01:17:44

标签: c# json entity-framework wcf

我使用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;
            }

        }

在browser / 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"

1 个答案:

答案 0 :(得分:0)

很容易: 首先创建一个这样的对象,并将其作为您的Web服务输出:

public class News
{
    public string field { get; set; }
    public string sortBy { get; set; }
    public Article[] articles { get; set; }
}
public class Article
{
    public string id { get; set; }
    public string title { get; set; }
    public string shortDescription { get; set; }
    public string urlToDescription { get; set; }
    public string urlToImage { get; set; }
    public string publishDate { get; set; }
    public string createDate { get; set; }
}

然后将您的查询结果解析为它。

将查询解析为此对象:

public class RootObject
{
    public string NewsServiceResult { get; set; }
}

2-将NewsServiceResult值解析为此对象的数组:

public class RootObject
{
    public string Id { get; set; }
    public string Title { get; set; }
    public string Image { get; set; }
    public string Description { get; set; }
    public string PublishDate { get; set; }
    public string CreatedDate { get; set; }
    public string UserName { get; set; }
    public string ShortDescription { get; set; }
}

3 - 最后你只需要枚举数组并将其放入输出中。