【问题标题】:Generate JSON array with WCF使用 WCF 生成 JSON 数组
【发布时间】:2012-04-09 13:24:26
【问题描述】:

我正在开发一个返回以下内容的 WCF Web 服务:

{
    "allFormsResult": [
        {
            "FormId": 1,
            "FormName": "Formulario 1"
        },
        {
            "FormId": 2,
            "FormName": "Formulario 2"
        },
        {
            "FormId": 3,
            "FormName": "Formulario 3"
        }
    ]
}

这是代码:

public class RestServiceImpl : IRestServiceImpl
    {
        public List<FormContract> allForms()
        {
            List<FormContract> list = null;
            using (var vAdmEntities = new ADMDatabase.ADMEntities())
            {
                list = new List<FormContract>();
                foreach (var form in vAdmEntities.Form)
                {
                    FormContract formC = new FormContract
                    {
                        FormName = form.name.Trim(),
                        FormId = form.formId
                    };
                    list.Add(formC);
                }
            }

            return list;
        }
    }

如何以这种方式生成它?

[
    {
        "FormId": 1,
        "FormName": "Formulario 1"
    },
    {
        "FormId": 2,
        "FormName": "Formulario 2"
    },
    {
        "FormId": 3,
        "FormName": "Formulario 3"
    }
]

【问题讨论】:

  • 不确定,但请尝试返回 FormContract[] 而不是 List&lt;FormContract&gt;

标签: c# json wcf rest


【解决方案1】:

问题出在这里:

namespace ADM
{
    [ServiceContract]
    public interface IRestServiceImpl
    {
        [OperationContract]
        [WebInvoke(Method = "GET",
            ResponseFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.Wrapped,
            UriTemplate = "forms/")]
        List<FormContract> allForms();
    }
}

我必须这样使用它:

namespace ADM
{
    [ServiceContract]
    public interface IRestServiceImpl
    {
        [OperationContract]
        [WebInvoke(Method = "GET",
            ResponseFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.Bare,
            UriTemplate = "forms/")]
        List<FormContract> allForms();
    }
}

更改BodyStyle

BodyStyle = WebMessageBodyStyle.Bare

【讨论】:

    【解决方案2】:

    这个行为也可以通过 Web.Config 设置为默认,不需要直接在合约中添加属性。

    <services>
      <service name="MyServiceNameSpace.MyServiceClass">
        <endpoint
            address="http://yourservicedomain.ext/MyServiceClass.svc/"
            binding="webHttpBinding"
            contract="MyServiceNameSpace.MyServiceContract"
            behaviorConfiguration="MyEndpointBehavoir"
            listenUri="/" />        
      </service>      
    </services>
    
    <behaviors>
      <endpointBehaviors>
        <behavior name="MyEndpointBehavoir">
          <webHttp defaultOutgoingResponseFormat="Json" defaultBodyStyle="Bare"/>
        </behavior>        
      </endpointBehaviors>
    </behaviors>
    

    【讨论】:

      猜你喜欢
      • 2022-06-15
      • 1970-01-01
      • 2016-04-15
      • 1970-01-01
      • 2014-03-29
      • 2016-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多