1

Есть json. Пытаюсь через Newtonsoftjson привести его к классу. Выдает ошибку. Вроде все делаю как раньше. Не пойму в чем ошибка

var m = JsonConvert.DeserializeObject<Rootobject>(json).Property1.Select(x=>x.value).ToList();

public class Rootobject
{
    public Class1[] Property1 { get; set; }
}

public class Class1
{
    public string value { get; set; }
    public string label { get; set; }
    public string firm { get; set; }
}

[
    {
        "value": "CL",
        "label": "CL",
        "firm": "ACURA"
    },
    {
        "value": "CSX",
        "label": "CSX",
        "firm": "ACURA"
    }
]

Newtonsoft.Json.JsonSerializationException: "Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Rootobject' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.

1 ответ 1

4

Вы же хотите получить в результате объект типа Rootobject. Вот такой JSON, соответствующий структуре этого класса, десериализуется:

{ "Property1": [
    {
        "value": "CL",
        "label": "CL",
        "firm": "ACURA"
    },
    {
        "value": "CSX",
        "label": "CSX",
        "firm": "ACURA"
    }
]}
2
  • ну так json с сервера приходит, мне выбирать не приходится
    – Radzhab
    16 фев 2017 в 19:37
  • 2
    JsonConvert.DeserializeObject<Class1[]>
    – user176262
    16 фев 2017 в 19:38

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.