Error deserializar JSON in .NET C # [closed]

-2

Does anyone know what he's trying to say about the error he makes?

    
asked by Samano Cedillo 25.05.2018 в 03:48
source

2 answers

1

The JSON you are trying to parse has a detail: the first character is a { key instead of a [ , so you will interpret an object instead of a array . The correct way to interpret it would be like this:

var jObject = JObject.Parse(json);              // Interpreta el JSON en un objeto sin clase
var jToken = jObject.GetValue("objecto");       // Obtienes el valor de "objecto"
Objecto obj = jToken.ToObject(typeof(Objecto)); // Lo parsea a una instancia de tu clase
List<Answer> answers = obj.answer;              // Accedes a la lista de "Answer"

Based on this answer in English .

    
answered by 26.05.2018 / 00:50
source
2

The json starts with a property called "Objecto" but you're trying to deserialize a json of type List<Objecto> and obviously this type does not have a property called asi.

Create a class that contains a property of type Lista<Objecto> :

public class MiJson{
  public List<Objeto> objeto { get; set;}
}

Then you would replace:

var answers = JsonConvert.DeserializeObject<List<Objeto>>(json);

By:

  var answers = JsonConverter.DeserializeObject<MiJson>(json).objeto;
    
answered by 25.05.2018 в 04:12