2012-06-27 26 views
9

tengo esta claselista de deserializar objetos usando json.net

public class Image 
{ 
    public string url { get; set; } 
    public string url_40px { get; set; } 
    public string url_50px { get; set; } 
} 

public class Category 
{ 
    public List<int> ancestor_ids { get; set; } 
    public int parent_id { get; set; } 
    public List<object> children_ids { get; set; } 
    public string nodename { get; set; } 
    public int num_parts { get; set; } 
    public List<Image> images { get; set; } 
    public string __class__ { get; set; } 
    public int id { get; set; } 
} 

y deserializar como este

retObject = JsonConvert.DeserializeObject(Of Category)(jsonResp) 

pero para una lista de categoría regresó, ¿Cómo se convierte a List<Category>? gracias

+2

ha intentado 'JsonConvert.DeserializeObject (Of List ) (jsonResp)' – Kane

Respuesta

18

El parámetro de tipo para DeserializeObject tiene que ser List<Category> en lugar de Category. No sé cómo escribir en VB, pero en C#, sería JsonConvert.DeserializeObject<List<Category>>(json).

+0

hola, ¿cuál es su array off 'category'? – Smith

+0

Puede usar * cualquier * tipo para el método 'DeserializeObject'. Newtonsoft intenta deserializar el tipo especificado y (en caso de éxito) lo devuelve. Por lo tanto, también puede escribir 'DeserializeObject (Of Category []) (json)'. (No sé VB, entonces quizás deba escribir el tipo de matriz diferente) – fero

+2

El VB para esto sería JsonConvert.DeserializeObject (Of List (Of Category)) (json) –

Cuestiones relacionadas