Mayur
Mayur

Reputation: 11

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 start and end. Because of which I can't use Json Parser tool. Please help.

Upvotes: 1

Views: 1266

Answers (1)

Oleg
Oleg

Reputation: 222017

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

Upvotes: 3

Related Questions