【问题标题】:How to format json data returned from EF entities in WCF service in standard json format?如何以标准 json 格式格式化从 WCF 服务中的 EF 实体返回的 json 数据?
【发布时间】: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&amp;nbsp;her VCE results.She was one of more than 2000 students who received their results early due to a&amp;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"

【问题讨论】:

标签: c# json entity-framework wcf


【解决方案1】:

很简单: 首先创建一个这样的对象并将其作为您的 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-最后,您只需要枚举数组并将其放入输出中。

【讨论】:

  • 能否提供更详细的解决方案?
  • 亲爱的@rudolf_franek。我更新了我的答案。希望它能解决你的问题。
猜你喜欢
  • 2018-04-22
  • 1970-01-01
  • 1970-01-01
  • 2012-09-19
  • 1970-01-01
  • 2017-04-14
  • 1970-01-01
  • 2013-10-23
  • 2011-06-11
相关资源
最近更新 更多