1

Hi: I have problems with deserialization Json: This is my Json(String):

{[
  {
    "IdGuid": "fac5d174-17d4-4330-a65e-07133e88e0ca",
    "Nombre": "Asignaturas",
    "Subtitulo": "Subtitulo de asignaturas",
    "Descripcion": "Descripcion de asignaturas",
    "urlFoto": "egio1.jpg"
  },
  [
    {
      "IdGuid": "a9a59e49-c318-4868-93a9-57347b4c4cad",
      "Nombre": "Ciencias Naturales",
      "Subtitulo": "",
      "Descripcion": "Ciencias",
      "urlFoto": "80.jpg"
    },
    {
      "IdGuid": "8ae0dc90-aa6a-4457-8e64-5f591f75416c",
      "Nombre": "Documentos",
      "Subtitulo": "",
      "Descripcion": "",
      "urlFoto": "asd.jpg"
    },
    {
      "IdGuid": "2ffbe004-316d-4a82-b4fe-0c43169766ad",
      "Nombre": "Inglés",
      "Subtitulo": "",
      "Descripcion": "",
      "urlFoto": "http://pue.jpg"
    },
    {
      "IdGuid": "62151f5c-f503-48a6-9801-c27e92aa240a",
      "Nombre": "Matemática",
      "Subtitulo": "",
      "Descripcion": "",
      "urlFoto": "http://pue.jpg"
    }
  ]
]}  

and this is my class:

public class Asignatura
    {
        public String idGuid { get; set; }

        public String nombre { get; set; }

        public String subtitulo { get; set; }

        public String descripcion { get; set; }

        public String urlFoto { get; set; }
    }

And I need generate a List of Asignaturas with de JSON. I'm trying with

List<Asignatura> listaAsignaturas = new List<Asignatura>();
listaAsignaturas= JsonConvert.DeserializeObject<List<Asignatura>>(json);

but don't work.

  • Please Help me with this.
  • I'm using Newtonsoft.Json

(edit) Adding Class:

public class rootAsignatura
{
    public Asignatura raiz;
    public List<Asignatura> listaAsignaturas;
}

and trying:

rootAsignatura listaAsignaturas = new rootAsignatura();
listaAsignaturas = JsonConvert.DeserializeObject<rootAsignatura>(json);

This continue without work.

4
  • 3
    Your JSON contains two items at top level: a single Asignatura object + an array of Asignatura objects. Can you fix it's structure to be just an array? That would make your deserialization code work. Commented Feb 5, 2015 at 17:55
  • 1
    @MarcinJuraszek You should post it as an answer:) Commented Feb 5, 2015 at 17:57
  • @MarcinJuraszek assuming his JSON format is fixed, Is there a way to deserialize this into a List of 2 elements: the first element being an Asignatura and the second element being a List<Asignatura>? Commented Feb 5, 2015 at 17:58
  • I can't remove anything because is response of webService Commented Feb 5, 2015 at 18:07

2 Answers 2

2

Your JSON String has 2 arrays one array with one "Asignatura"which has another nested array of "Asignatura". Remove the second set of "[]" brackets.

Sign up to request clarification or add additional context in comments.

4 Comments

I can't remove anything because is response of webService.
Then you need to change your object you are deserializing to. In particular, you get a list of <asignatura, list of asignatura> so your object you read needs to add on a List<Asignatura> object or something.
Matt thanks for response, I edit the question adding new class but continue without work.
Im pretty sure the webserver is giving invalid JSON, should contact the service owner. Also, Andrew Whitaker has a reasonable solution.
0

First, I'd contact the owner of the webservice and tell them they're serving invalid JSON.

The outer {...} that's there currently means that they're serving an Object, but that object has no keys and values (which are required), it just wraps an Array.

Anyway, you could get around this by trimming off the { and } at the beginning and end of the string. Then you're left with an Array whose first item is an Asignatura and the second is an Array of Asignaturas.

If you just want one List<Asignatura>, the easiest way is probably to deserialize into a JArray and then parse the individual elements:

/* Parse the underlying array after removing the opening and closing braces */
var array = JArray.Parse(json.Trim('{', '}'));

/* Deserialize the first item in the array */  
var signatureOne = array[0].ToObject<Asignatura>();

/* Deserialize the second item in the array as a List<Asignatura> */
List<Asignatura> signatureList = array[1].ToObject<List<Asignatura>>();

/* Add the first item to the array. You could also use Insert(0, signatureOne) to 
 * put it at the front. */
signatureList.Add(signatureOne);

4 Comments

Andrew this work perfectly, you are a genius. I try this to return a List<Asignatura> List<Asignatura> listAsignaturas = new List<Asignatura>(); listAsignaturas = (List<Asignatura>)signatureList; and work, is good?
That seems fine, but I will update the code to make signatureList a List<Asignatura> instead, that'll be a little cleaner.
this work perfect, I only have a question... Why use json.Trim('{', '}') in the solution?
@KevinF: Sure, Because using { and } indicates an Object in JSON. Objects have a very specific definition in JSON, specifically they are an unordered set of key/value pairs. The data inside of the {...} in your sample doesn't contain key/value pair, it contains an array, making it invalid JSON. The easiest way to make it valid is to simply remove the outer {...}.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.