在C#中使用DataContractJsonSerializer对具有List <>的JSON对象进

Deserialization of JSON object with List<> using DataContractJsonSerializer in C#
2021-07-21
  •  译文(汉语)
  •  原文(英语)

我想反序列化包含单个成员的JSON对象;字符串数组:

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

这是我要反序列化的类:

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

这是我尝试反序列化的方法:

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

但是当我检查时,子列表的所有字段均为空白,例如

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

我想一定是我想念的东西=(

速聊1:
你试过了System.Web.Script.Serialization.JavascriptSerializer吗?
解决过程1

您的模型需要具有与JSON对象名称匹配的属性:

[DataContract]
protected internal struct Errors
{
    [DataMember]
    public string errorCode { get; set; }
    [DataMember]
    public string errorMessage { get; set; }
}
速聊1:
我建议使用DataMemberName属性.它使"无害"重构不太可能破坏现有合同.
速聊2:
如果为您找到了答案,那么您通常会接受它,以便将来的访问者可以快速看到它可以解决问题.
解决过程2

这可以为您提供帮助.



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


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 = (

Talk1:
Have you try System.Web.Script.Serialization.JavascriptSerializer ?
Solutions1

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; }
}
Talk1:
I recommend using the Name property of DataMember. It makes "harmless" refactors less-likely to break existing contracts.
Talk2:
If an answer works out for you, you typically accept it so future visitors can quickly see that it solves the problem.
Solutions2

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


转载于:https://stackoverflow.com/questions/17414018/deserialization-of-json-object-with-list-using-datacontractjsonserializer-in-c

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入