简体   繁体   中英

Can't deserialize Dictionary from JSON

I have this class:

class SoporteFile 
    {
        public Evento[] eventos { get; set; }
        public Programacion[] programaciones { get; set; }
        public static Dictionary<int, object[]> soportes { get; set; }
    }

Then I have the Soporte

 class Soporte
    {
        public Aviso[] avisos { get; set; }
        public Placa[] placas { get; set; }
        public Portada[] portadas { get; set; }
        public Efemeride[] efemerides { get; set; }
        public Comunicado[] comunicados { get; set; }
    }

The class Dictionary from SoporteFile, should match the class Soporte. soportes[0] should have an Aviso array, soportes[1] shoud have a Placa array, etc. When I parse it with Json.NET, even If there's no relation between Dictionary soportes and Soporte class, I get no error, it seems to be parsing well (I don't know if it's), but when I try to access Dictionary soportes like this (first deserialize):

 SoporteFile curContent = JsonConvert.DeserializeObject<SoporteFile>(File.ReadAllText(curFolder + @"/Contenido.json"));

curContent.soportes[0] I get an error (Error CS0176 Member cannot be accessed with an instance reference; qualify it with a type name instead). And if I do this SoporteFile.soportes[0] I get Reference not established as an instance of an object (this last error translated from Spanish, it can be slightly different). What I'm doing wrong?

If it's need it, here's the json I try to deserialize:

{
"eventos": [
    {
        "id_evento": 5,
        "id_calendario": 5,
        "id_usuario": 1,
        "fecha_carga": "2017-04-25 08:47:20",
        "descripcion": "Mañanas",
        "lugar": null,
        "resumen": null,
        "fecha_inicio": "2017-05-16",
        "hora_inicio": "06:00:00",
        "fecha_limite": "2017-05-16",
        "hora_limite": "12:00:00",
        "dia_completo": 0,
        "id_estado": null,
        "recordatorio": null,
        "bloqueado": 0,
        "id_programacion": 1,
        "color": null
    },
    {
        "id_evento": 8,
        "id_calendario": 5,
        "id_usuario": 1,
        "fecha_carga": "2017-04-27 16:51:56",
        "descripcion": "Tarde",
        "lugar": null,
        "resumen": null,
        "fecha_inicio": "2017-05-16",
        "hora_inicio": "12:00:00",
        "fecha_limite": "2017-05-16",
        "hora_limite": "20:00:00",
        "dia_completo": 0,
        "id_estado": null,
        "recordatorio": null,
        "bloqueado": 0,
        "id_programacion": 1,
        "color": null
    },
    {
        "id_evento": 5,
        "id_calendario": 5,
        "id_usuario": 1,
        "fecha_carga": "2017-04-25 08:47:20",
        "descripcion": "Mañanas",
        "lugar": null,
        "resumen": null,
        "fecha_inicio": "2017-05-17",
        "hora_inicio": "06:00:00",
        "fecha_limite": "2017-05-17",
        "hora_limite": "12:00:00",
        "dia_completo": 0,
        "id_estado": null,
        "recordatorio": null,
        "bloqueado": 0,
        "id_programacion": 1,
        "color": null
    },
    {
        "id_evento": 8,
        "id_calendario": 5,
        "id_usuario": 1,
        "fecha_carga": "2017-04-27 16:51:56",
        "descripcion": "Tarde",
        "lugar": null,
        "resumen": null,
        "fecha_inicio": "2017-05-17",
        "hora_inicio": "12:00:00",
        "fecha_limite": "2017-05-17",
        "hora_limite": "20:00:00",
        "dia_completo": 0,
        "id_estado": null,
        "recordatorio": null,
        "bloqueado": 0,
        "id_programacion": 1,
        "color": null
    },
    {
        "id_evento": 5,
        "id_calendario": 5,
        "id_usuario": 1,
        "fecha_carga": "2017-04-25 08:47:20",
        "descripcion": "Mañanas",
        "lugar": null,
        "resumen": null,
        "fecha_inicio": "2017-05-18",
        "hora_inicio": "06:00:00",
        "fecha_limite": "2017-05-18",
        "hora_limite": "12:00:00",
        "dia_completo": 0,
        "id_estado": null,
        "recordatorio": null,
        "bloqueado": 0,
        "id_programacion": 1,
        "color": null
    },
    {
        "id_evento": 8,
        "id_calendario": 5,
        "id_usuario": 1,
        "fecha_carga": "2017-04-27 16:51:56",
        "descripcion": "Tarde",
        "lugar": null,
        "resumen": null,
        "fecha_inicio": "2017-05-18",
        "hora_inicio": "12:00:00",
        "fecha_limite": "2017-05-18",
        "hora_limite": "20:00:00",
        "dia_completo": 0,
        "id_estado": null,
        "recordatorio": null,
        "bloqueado": 0,
        "id_programacion": 1,
        "color": null
    }
],
"programaciones": [
    {
        "id_programacion": 1,
        "nombre": "Programación estándar",
        "tipo_corte": 1,
        "duracion": null,
        "soportes": [
            {
                "id_soporteprogramacion": 10,
                "id_programacion": 1,
                "soporte": 2,
                "orden": null,
                "cantidad": 10,
                "duracion": null
            },
            {
                "id_soporteprogramacion": 11,
                "id_programacion": 1,
                "soporte": 3,
                "orden": 1,
                "cantidad": 12,
                "duracion": null
            },
            {
                "id_soporteprogramacion": 12,
                "id_programacion": 1,
                "soporte": 1,
                "orden": 2,
                "cantidad": 5,
                "duracion": null
            },
            {
                "id_soporteprogramacion": 13,
                "id_programacion": 1,
                "soporte": 12,
                "orden": 3,
                "cantidad": 1,
                "duracion": null
            },
            {
                "id_soporteprogramacion": 14,
                "id_programacion": 1,
                "soporte": 7,
                "orden": 4,
                "cantidad": 5,
                "duracion": null
            },
            {
                "id_soporteprogramacion": 15,
                "id_programacion": 1,
                "soporte": 13,
                "orden": 5,
                "cantidad": 1,
                "duracion": null
            },
            {
                "id_soporteprogramacion": 16,
                "id_programacion": 1,
                "soporte": 8,
                "orden": 6,
                "cantidad": 10,
                "duracion": null
            }
        ]
    }
],
"soportes": {
    "2": [
        {
            "id_placa": 2,
            "id_ref": 1,
            "timestamp": "2017-05-16 19:38:35",
            "id_region": 1,
            "vigencia_desde": "2017-04-12 08:13:00",
            "vigencia_hasta": "2018-04-12 08:13:00",
            "duracion": 1,
            "imagen": "17202847_10212332856146238_6497670072350234982_n.jpg",
            "texto": "el texto",
            "orden": 0,
            "audio": null
        },
        {
            "id_placa": 3,
            "id_ref": 2,
            "timestamp": "2017-05-16 19:38:35",
            "id_region": 1,
            "vigencia_desde": "2017-04-12 08:13:00",
            "vigencia_hasta": "2018-04-12 08:13:00",
            "duracion": 1,
            "imagen": null,
            "texto": "sin imagen",
            "orden": 0,
            "audio": null
        }
    ],
    "1": [
        {
            "id_aviso": 7,
            "id_ref": 2,
            "timestamp": "2017-05-16 19:38:35",
            "id_region": 1,
            "vigencia_desde": "2017-05-16 19:00:00",
            "vigencia_hasta": "2018-05-16 23:59:00",
            "titulo": "RENAULT MEGANE",
            "id_categoriaaviso": 1,
            "texto": "TEXTO DEL AVISO",
            "contacto": null,
            "imagen": null
        },
        {
            "id_aviso": 8,
            "id_ref": 4,
            "timestamp": "2017-05-16 19:38:35",
            "id_region": 1,
            "vigencia_desde": "2017-05-16 19:00:00",
            "vigencia_hasta": "2018-05-16 23:59:00",
            "titulo": "FORD FOCUS",
            "id_categoriaaviso": 1,
            "texto": "FORD",
            "contacto": null,
            "imagen": "Apertura_2008_La_Capital_Rosario.jpg"
        }
    ],
    "13": {
        "id_region": 1,
        "timestamp": "2017-05-16 19:38:35",
        "fecha": "2017-05-16",
        "descripcion": "El aporte de aire húmedo desde el norte mantiene abundante nubosidad y menor amplitud térmica, mientras que sobre la región Patagónica ingresa un frente frío dando lugar a algunas precipitaciones y descenso de temperatura. "
    },
    "7": [
        {
            "id_portada": 1,
            "timestamp": "2017-05-16 19:38:35",
            "id_region": 1,
            "nombre": "La nación",
            "url": "\\1.jpg"
        },
        {
            "id_portada": 2,
            "timestamp": "2017-05-16 19:38:35",
            "id_region": 1,
            "nombre": "Clarín",
            "url": "\\2.jpg"
        }
    ],
    "8": [
        {
            "id_efemeride": 6,
            "id_ref": 2,
            "timestamp": "2017-05-16 19:38:35",
            "id_region": 1,
            "fecha": "2017-05-17",
            "titulo": "Día del enfermero 2",
            "texto": "En enero de 1974, se tomó la decisión de celebrar una jornada conmemorativa de las contribuciones de los enfermeros a la sociedad, promovida por el Consejo Internacional de Enfermería, que se celebra en todo el mundo cada 12 de mayo, conmemorando el natalicio de Florence Nightingale, considerada \\\"fundadora\\\" de la enfermería moderna. Nacida en 1820, de fe anglicana, creía que Dios la había inspirado para ser enfermera. Alcanzó fama mundial por sus trabajos precursores de enfermería en la asistencia a los heridos durante la guerra de Crimea.",
            "imagen": null,
            "video": null,
            "duracion": 10
        }
    ]
}

}

EDIT : I probably need to take out the static from Dictionary. If I do that, I get this error: "Cannot deserialize the current JSON object (eg {"name":"value"}) into type 'System.Object[]' because the type requires a JSON array (eg [1,2,3]) to deserialize correctly."

试试这个 - 对我运行代码起作用:(为什么你在类型中指定了一个对象数组?)

public Dictionary<int, object> soportes { get; set; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM