开发者

WCF Service JSON Data

I've created a WCF Web Service which returns data in JSON format. The code for the service is as follows:

[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json)]
List<MyCustomer> GetCustomerJSON();

And

public List<MyCustomer> GetCustomerJSON()
    {
        var nm = (from n in _ent.Customers
                  select new MyCustomer() { CustomerID = n.CustomerID, AccountNumber = n.AccountNumber }).Take(10);

        return nm.ToList();

    }

However, the output isn't well formed. It included square brackets at 开发者_JAVA技巧start and end. Because of which I can't use Json Parser tool. Please help.


If you return List<T> it will be encoded like array of T in the JSON and array will be encoded with respect of square brackets:

[{"strProprety":"bla","intProperty":123,"booleanProperty":true}]

In your case it will be probably

[{"CustomerID":1,"AccountNumber":123},{"CustomerID":2,"AccountNumber":456}]

It is valid JSON. You can use http://www.jsonlint.com/ to verify this. So WCF produce correct output and you have problem only with the "Json Parser tool".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜