0

I have a REST service which accepts JSON data from client. I am able to retrieve the JSON input from client using .Net class. But I want to retrieve the data in JSON string format, not as a class object.

This is what I have tried so far.

input JSON

<input id="Text3" type="text" value='{ "searchBy": "Pending Cases", "displayOptions": [ {"producers": "yes", "GA/BGA/Firm": "yes"}],"userId": "xxx", "userAuthToken": "0000" }' /></p>

calling service via javascript

function CallService()
    {
        var inputJSON = $("#Text3").val();
        var endpointAddress = $("#Text1").val();
        var url = endpointAddress + $("#Text2").val();

        $.ajax({
            type: 'POST',
            url: url,
            contentType: 'application/json',
            data: inputJSON,
            success: function (result) {                    
                $("#Text4").val(" " + JSON.stringify(result));
            }
        });

    }

Service side - contract

[OperationContract]
    [WebInvoke(
        Method = "POST",
        BodyStyle = WebMessageBodyStyle.Bare,
        RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json)]
    string SearchPredefined(PredefinedSearchRequestModel PredefinedSearchRequest);

implementation of service method

public string SearchPredefined(PredefinedSearchRequestModel PredefinedSearchRequest)
    {
        string outputStr = "PredefinedSearchRequest Object gets successfully populated here ";
        return outputStr;
    }

Model class

[DataContract]
public class PredefinedSearchRequestModel
{
    [DataMember]
    public string searchBy { get; set; }

    [DataMember]
    public List<displayOptionsModelPredefined> displayOptions { get; set; }

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

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

}

[DataContract]
[Serializable]
public class displayOptionsModelPredefined
{
    [DataMember]
    public string producers { get; set; }

    [DataMember(Name="GA/BGA/Firm")]
    public string firm { get; set; }
}

So far these code works fine. when client calls my service with JSON data, the service method gets hit and the model object is successfully populated.

However I need to call another 3rd party service with the same JSON string from my service. this is why I need the input data in the Raw JSON/string format, not as an C# object.

How can I get the data in server side as JSON string ?

Bluemarble
  • 1,925
  • 4
  • 20
  • 35

1 Answers1

0

I recommend serializing the object in SearchPredefined using NewtonSoft and passing the result through:

var serializedObject = Newtonsoft.Json.JsonConvert.SerializeObject<PredefinedSearchRequestModel>(PredefinedSearchRequest);
Tim Smojver
  • 116
  • 6