0

I have a very simple DataContract class that I need to deserialise into. I'm not using JSON.NET for a particular reason so use DataContractJsonSerializer.

My contract class looks like this

[DataContract(Name = "d")]
    public class ResponseClass
    {
        [DataMember]
        public int ResponseCode
        { get; set;}
        [DataMember]
        public string ResponseMessage
        { get; set;}
        [DataMember]
        public string Response
        { get; set;}
    }

The service call is coming back with a valid Json string, but in deserialising, the value at the end is always 0 or string.Empty despite the value being passed in being non-0 or non string.Empty.

My deserialise code looks like this

    private string deserializeJSON(string resp, bool code = false)
    {
        string value = string.Empty;
        ResponseClass deserialized = null;
        using (MemoryStream stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(resp)))
        {
            stream.Position = 0;
            var ser = new DataContractJsonSerializer(typeof(ResponseClass));
            deserialized = ser.ReadObject(stream) as ResponseClass;
        }
        value = code ? deserialized.Response : deserialized.ResponseCode.ToString();
        return value;
    }

I've looked around for an answer on this but nothing is springing back.

The data back looks like this (which is why the contract back is called "d" - not sure if that makes a difference or not, but the result is still the same, RespCode is 0, RespMess,Resp = string.Empty)

{ 
    "d":
    {
         "RespCode": 0,
         "RespMess": null,
         "Resp": null
    }
}
1
  • 1
    I'm sorry, but your answer doesn't have enuf details for me to fully understand your problem. What do you mean by "the value at the end" is always zero or string.Empty? Do you mean the last data member, "Response" property? By "despite the value being passed in", do you mean the value being passed into your private string deserializeJSON(string resp, bool code = false) method? Commented Nov 4, 2013 at 17:05

1 Answer 1

1

Since your C# class properties (e.g. ResponseCode) and JSON names (e.g. RespCode) don't match, you need to specify those Names in your DataMember attributes. And the structure with the d could be represented by another class (I made ResponseClassContainer for it), not by simply naming the DataContract d. This works:

private string deserializeJSON(string resp, bool code = false)
{
    string value = string.Empty;
    ResponseClass deserialized = null;
    using (MemoryStream stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(resp)))
    {
        stream.Position = 0;
        var ser = new DataContractJsonSerializer(typeof(ResponseClassContainer));
        deserialized = (ser.ReadObject(stream) as ResponseClassContainer).d;
    }
    value = code ? deserialized.Response : deserialized.ResponseCode.ToString();
    return value;
}
[DataContract]
public class ResponseClassContainer
{
    [DataMember]
    public ResponseClass d { get; set; }
}
[DataContract]
public class ResponseClass
{
    [DataMember(Name="RespCode")]
    public int ResponseCode
    { get; set;}
    [DataMember(Name="RespMess")]
    public string ResponseMessage
    { get; set;}
    [DataMember(Name="Resp")]
    public string Response
    { get; set;}
}

The 0 integer and null strings were present because those are the default values for their respective types. Since the string didn't have any recognized data, those were all that were ever set there.

Sign up to request clarification or add additional context in comments.

1 Comment

I see - thank's for the explanation. As I say, normally, I'd just use JSON.NET for this as it makes for a simple life, but I have a specific need to do it this way - thanks for the answer :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.