2016-12-15 144 views
0

我已经在其关于浏览器/场=新闻服务请求执行如何格式化标准JSON格式的WCF服务中的EF实体返回的JSON数据?

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; 
      } 

     } 

使用VS2015

[ServiceContract] 
    public interface ISchoolProjectService 
    {  
    [OperationContract] 
     [WebInvoke(Method = "GET", 
      ResponseFormat = WebMessageFormat.Json, 
      RequestFormat =WebMessageFormat.Json,   
      UriTemplate = "field=news/{id}")] 
     IList<Object> NewsService(string id); 


    } 

创建的WCF Web服务方法如下套/所有 我获得以下响应

{"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" 

and Image into

"urlToImage":"http://webapischoolproject.yarshatech.com/Detail/NewsAndAnnouncement/47c05ca9-d126-4823-8e16-17d499c78b5d.jpg" 
+0

如果您在服务的端点行为中添加,则应该使用不包装的json。签出这个博客:http://blog.clauskonrad.net/2010/11/how-to-expose-json-endpoint-from-wcf.html –

回答

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; } 
} 

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

1-解析查询在此对象:

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-最后你只需要枚举阵列并把它放到你的输出中。

+0

你能否提供更详细的解决方案? –

+0

亲爱的@rudolf_franek。我更新了我的答案。 ihope它解决了你的问题。 – David