Ошибка при обработке Json - Cannot deserialize the current JSON array because the type requires a JSON object - C# (232725)

Узнай цену своей работы

Формулировка задачи:

Всем привет! Помогите, плиз, разобраться с ошибкой:

An unhandled exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll Additional information: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Test.Form_Main+RootObject' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.

Листинг программы
  1. public class RootObject
  2. {
  3. public string test { get; set; }
  4. public string test2 { get; set; }
  5. public string test3 { get; set; }
  6. }
  7. string json = "[{ "test":"12213","test2":"24242","test3":"34535"]";
  8. RootObject newM = JsonConvert.DeserializeObject<RootObject>(json);
  9. rTB_Status.Text = newM.test;
Полный текст ошибки

Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Test.Form_Main+RootObject' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. Path '', line 1, position 1.

Нашла решение - нужно убрать квадратные скобки ][ Кто-нибудь подскажет, с помощью какой команды убрать из строки квадратные скобки? Строка будет приходить с квадратными скобками, вручную не получится редактировать.

Решение задачи: «Ошибка при обработке Json - Cannot deserialize the current JSON array because the type requires a JSON object»

textual
Листинг программы
  1. RootObject newM = JsonConvert.DeserializeObject<RootObject>(json);
  2. rTB_Result.Text = newM.test;

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

13   голосов , оценка 3.923 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы