I am having a json string
json1 = "{\"Shops\":[{\"id\":\"1\",\"title\":\"AAA\"},{\"id\":\"2\",\"title\":\"BBB\"}],\"movies\":[{\"id\":\"1\",\"title\":\"Sherlock\"},{\"id\":\"2\",\"title\":\"The Matrix\"}]}";
want to deserialize and get values in two classes shops and movies inside requestcollectionclass.
[WebInvoke(
Method = "POST",
UriTemplate = "SubmitRequest",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json
)
]
string SubmitRequest(string RequestDetailsjson);
and in the service.cs
JavaScriptSerializer serializer = new JavaScriptSerializer();
RequestDetailsCollection requestdetailscoll = serializer.Deserialize<RequestDetailsCollection>(RequestDetailsjson);
Getting error
The server encountered an error processing the request. The exception message is 'There was an error deserializing the object of type System.String. End element 'root' from namespace '' expected. Found element 'request' from namespace ''.'. See server logs for more details. The exception stack trace is:
I changed the parameter type to stream
string SubmitRequest(Stream RequestDetailsjson);
And changed the code
StreamReader sr = new StreamReader(RequestDetailsjson);
JavaScriptSerializer serializer = new JavaScriptSerializer();
RequestDetailsCollection requestdetailscoll = (RequestDetailsCollection)serializer.DeserializeObject(sr.ReadToEnd());
And getting error
The server encountered an error processing the request. The exception message is 'Incoming message for operation 'SubmitRequest' (contract 'IService1' with namespace 'http://tempuri.org/') contains an unrecognized http body format value 'Json'. The expected body format value is 'Raw'. This can be because a WebContentTypeMapper has not been configured on the binding. See the documentation of WebContentTypeMapper for more details.'. See server logs for more details. The exception stack trace is:
Help me to resolve this issue
Since there is no code for the
RequestDetailsCollection
I used json2csharp to convert your json to an object:Deserializing like this:
Returned:
I was getting this error too, here's how I fixed it:
I wanted a Stream too and when a
Content-Type: application/json
is sent it was breaking (was ok if no content-type specified i.e. Raw). I wanted it to work if the other party specified the content-type or not.So create this file
RawContentTypeMapper.cs
and paste the following inIn your web.config you need to specify it to be used (all the following in the
<system.serviceModel>
section):you'll need to update your service to use this too:
here's my behaviours section too for completeness
Long question short, you wanted to know how to deserialise your json message. This is how to do it.
Your entity classes should be like this. (Code Generated using Paste Special)
Thanks for the reply . I have two list<> of different classes inside one class i want to pass json string and serialize .
And in service code JavaScriptSerializer serializer = new JavaScriptSerializer(); RequestDetailsCollection requestdetailscoll = serializer.Deserialize(RequestDetailsjson);
Getting error in POSTMAN REST Client The server encountered an error processing the request. The exception message is 'There was an error deserializing the object of type System.String. End element 'root' from namespace '' expected. Found element 'request' from namespace ''.'. See server logs for more details. The exception stack trace is:
Passing stream param like string SubmitRequest(Stream RequestDetailsjson); Getting Below error
The server encountered an error processing the request. The exception message is 'Incoming message for operation 'SubmitRequest' (contract 'IService1' with namespace 'http://tempuri.org/') contains an unrecognized http body format value 'Json'. The expected body format value is 'Raw'. This can be because a WebContentTypeMapper has not been configured on the binding. See the documentation of WebContentTypeMapper for more details.'. See server logs for more details. The exception stack trace is: