1

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

{"errores":[{"errorCode":"campo1","errorMessage":"Errorenelcampo1"},{"errorCode":"campo2","errorMessage":"Errorenelcampo2"},{"errorCode":"campo3","errorMessage":"Errorenelcampo3"},{"errorCode":"campo4","errorMessage":"Errorenelcampo4"}]}"

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

[DataContract]
internal class rptaNo
{
    [DataMember]
    public List<Errors> errores { get; set; }

    [DataContract]
    protected internal struct Errors
    {
        [DataMember]
        public string codigo { get; set; }
        [DataMember]
        public string descripcion { get; set; }
    }
}

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;
    }
}

But when I check, all fields of the sub list are blank, for example

MessageBox.Show(deserializedRpta2.errores[0].descripcion);

I guess there must be something I'm missing = (

dbc
  • 104,963
  • 20
  • 228
  • 340
stvn03
  • 43
  • 2
  • 2
  • 5
  • Have you try `System.Web.Script.Serialization.JavascriptSerializer` ? – Olrac Jul 02 '13 at 07:25
  • possible duplicate of [Deserialization of JSON object by using DataContractJsonSerializer in C#](http://stackoverflow.com/questions/17409655/deserialization-of-json-object-by-using-datacontractjsonserializer-in-c-sharp) – Peter Mularien Aug 11 '14 at 18:52

2 Answers2

4

your model needs to have properties that match the names of JSON objects:

[DataContract]
protected internal struct Errors
{
    [DataMember]
    public string errorCode { get; set; }
    [DataMember]
    public string errorMessage { get; set; }
}
wut wut
  • 41
  • 3
  • 1
    I recommend using the [Name property of DataMember](http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datamemberattribute.name.aspx). It makes "harmless" refactors less-likely to break existing contracts. – user2246674 Jul 01 '13 at 21:32
  • @stvn03 If an answer works out for you, you typically accept it so future visitors can quickly see that it solves the problem. – Asad Saeeduddin Apr 29 '14 at 12:04
0

This can help you.


    public class KeyValue
    {
        public string errorCode
        {
            get; set;
        }

        public string errorMessage
        {
            get; set;
        }
    }

    string json = @"[{""errorCode"":""code1"",""errorMessage"":""value1""}, {""errorCode"":""code2"",""errorMessage"":""value2""}, {""errorCode"":""code3"",""errorMessage"":""value3""}] ";
    JavaScriptSerializer deserializer = new JavaScriptSerializer();
    List items = deserializer.Deserialize>(json);

    foreach(var item in items)
    {
        Console.WriteLine(item.errorCode + ":" + item.errorMessage);
    }


Alexei Bondarev
  • 812
  • 7
  • 9