1

I have WCF REST service returns a JSON response like

{
    "categories": [{
        "category_id": "10",
        "name": "Grocery",
        "freeQnty":"0",
        "prdcost":"100"
    }, {
        "category_id": "20",
        "name": "Beverages",
        "freeQnty":"1",
        "prdcost":"20"  
    }]
}

But i want response with service status like.

{
    "success": true,
    "categories": [{
        "category_id": "10",
        "name": "Grocery",
        "freeQnty":"0",
        "prdcost":"100"
    }, {
        "category_id": "20",
        "name": "Beverages",
        "freeQnty":"1",
        "prdcost":"20"  
    }]
}

and this is my services.

[OperationContract]
        [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "json/GetCustomerDetails/{customerid}")]
        Merchant GetCustomerDetails(string customerid);


[DataContract]
    public class categories
    {
        [DataMember]
        public int category_id{ get; set; }

        [DataMember]
        public string name { get; set; }

        [DataMember]
        public int freeQnty{ get; set; }

        [DataMember]
        public int prdcost { get; set; }
    }

how to get that success status if service success i need to show "success": true other wise "success": false.

surendra
  • 63
  • 9
  • Is using the HttpStatusCodes to denote success not an option? E.g. 200 (Ok) or 400 (Bad Request) etc – Chima Osuji Feb 10 '16 at 10:48
  • Possible duplicate of [how to join two different JSON into single JSON in C#](http://stackoverflow.com/questions/35039631/how-to-join-two-different-json-into-single-json-in-c-sharp) – Randhi Rupesh Feb 11 '16 at 10:05
  • solution is http://stackoverflow.com/questions/9235743/return-json-array-with-name-from-wcf-with-service – surendra Mar 01 '16 at 05:03

2 Answers2

0

Possible variant:

[OperationContract]
[WebInvoke(
BodyStyle = WebMessageBodyStyle.Bare, //or WrappedRequest
Method = "GET",
ResponseFormat = WebMessageFormat.Json,     
UriTemplate = "/somemethod?param1={param1}&param2={param2}")]
System.ServiceModel.Channels.Message SomeMethod(string param1, string param2)
{
    // use JSON.NET to add missing properties etc: 
    var jObject = JObject.FromObject(yourObject); 
    jObject["success"] = true; 
    var json = jObject.ToString();

    WebOperationContext.Current.OutgoingResponse.ContentType = "application/json";
    return WebOperationContext.Current.CreateTextResponse(json);
}
SalientBrain
  • 2,431
  • 16
  • 18
0

Better to change your response contract. Create a new response class with members success and categories as below and return that

  [DataContract]
  public class YourResponse
{
    [DataMember]
    public bool Success { get; set; }

    [DataMember]
    public categories Categories{ get; set; }
}
Krazibit312
  • 380
  • 2
  • 12