Deserialization of JSON object by using DataContractJsonSerializer in C#

Posted by user2539667 on Stack Overflow See other posts from Stack Overflow or by user2539667
Published on 2013-07-01T16:49:01Z Indexed on 2013/07/01 17:06 UTC
Read the original article Hit count: 548

enter code hereI'm sure this question has been asked over and over again, but for some reason, I still can't manage to get this to work.

I want to deserialize a JSON object that contains a single member; a string array:

[{"idTercero":"cod_Tercero"}]

This is the class that I'm trying to deserialize into:

[DataContract]
public class rptaOk
{
    [DataMember]
    public string idTercero { get; set; }


    public rptaOk() { }

    public rptaOk(string idTercero)
    {
        this.idTercero = idTercero;
    }
}

This is the method that I try to deserialize:

public T Deserialise<T>(string json)
    {
        DataContractJsonSerializer deserializer = new DataContractJsonSerializer(typeof(T));
        using (MemoryStream stream = new MemoryStream(Encoding.Unicode.GetBytes(json)))
        {
            T result = (T)deserializer.ReadObject(stream);
            return result;
        }
    }

And so try to fill the object:

rptaOk deserializedRpta = deserializarOk(rpta);

But for some reason, this returns ""

MessageBox.Show(deserializedRpta.idTercero);

© Stack Overflow or respective owner

Related posts about c#

Related posts about JSON