Note that there are some explanatory texts on larger screens.

plurals
  1. POGeneric WCF JSON Deserialization
    primarykey
    data
    text
    <p>I am a bit new to WCF and will try to clearly describe what I am trying to do.</p> <p>I have a WCF webservice that uses JSON requests. I am doing fine sending/receiving JSON for the most part. For example, the following code works well and as expected.</p> <p>JSON sent:</p> <pre><code>{ "guy": {"FirstName":"Dave"} } </code></pre> <p>WCF:</p> <pre><code> [DataContract] public class SomeGuy { [DataMember] public string FirstName { get; set; } } [OperationContract] [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] public string Register(SomeGuy guy) { return guy.FirstName; } </code></pre> <p>This returns a JSON object with "Dave" as expected. The problem is that I cannot always guarantee that the JSON I recieve will exactly match the members in my DataContract. For example, the JSON: </p> <pre><code>{ "guy": {"firstname":"Dave"} } </code></pre> <p>will not serialize correctly because the case does not match. guy.FirstName will be null. This behavior makes sense, but I don't really know how to get around this. Do I have to force the field names on the client or is there a way I can reconcile on the server side?</p> <p>A possibly related question: can I accept and serialize a generic JSON object into a StringDictionary or some kind of simple key value structure? So no matter what the field names are sent in the JSON, I can access names and values that have been sent to me? Right now, the only way I can read the data I'm receiving is if it exactly matches a predefined DataContract.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload