Ошибка при десериализации объекта C# с помощью Newtonsoft. Json: Ошибка преобразования значения «xxx» в тип «System.String []» - PullRequest
0 голосов
/ 05 августа 2020

Я впервые использую Newtonsoft. Json, и у меня возникла проблема с привязкой моих JSON объектов к моим C#.

Вот определение моего объекта:

namespace pg.Factories
{
    using System;

    using System.Globalization;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Converters;
    public partial class Survey
    {
        [JsonProperty("id")]
        public string Id { get; set; }

        [JsonProperty("title")]
        public string Title { get; set; }

        [JsonProperty("created_at")]
        public DateTimeOffset CreatedAt { get; set; }

        [JsonProperty("updated_at")]
        public DateTimeOffset UpdatedAt { get; set; }

        [JsonProperty("collectors")]
        public string[][] Collectors { get; set; }

        [JsonProperty("pages")]
        public string[][] Pages { get; set; }

        [JsonProperty("options")]
        public Options Options { get; set; }

        [JsonProperty("links")]
        public Links Links { get; set; }

        [JsonProperty("meta")]
        public Meta Meta { get; set; }
    }

    public partial class Options
    {
        [JsonProperty("display_progress_bar")]
        public bool DisplayProgressBar { get; set; }

        [JsonProperty("display_title")]
        public bool DisplayTitle { get; set; }

        [JsonProperty("footer_text")]
        public string FooterText { get; set; }

        [JsonProperty("language")]
        public string Language { get; set; }

        [JsonProperty("display_logo")]
        public bool DisplayLogo { get; set; }

        [JsonProperty("display_pages_numbering")]
        public bool DisplayPagesNumbering { get; set; }

        [JsonProperty("display_questions_numbering")]
        public bool DisplayQuestionsNumbering { get; set; }

        [JsonProperty("correction_type")]
        public string CorrectionType { get; set; }

        [JsonProperty("timer")]
        public Timer Timer { get; set; }
    }

    public partial class Timer
    {
        [JsonProperty("hours")]
        public long Hours { get; set; }

        [JsonProperty("minutes")]
        public long Minutes { get; set; }

        [JsonProperty("seconds")]
        public long Seconds { get; set; }
    }

    public partial class Links
    {
        [JsonProperty("first")]
        public string First { get; set; }

        [JsonProperty("last")]
        public string Last { get; set; }

        [JsonProperty("prev")]
        public string Prev { get; set; }

        [JsonProperty("next")]
        public string Next { get; set; }
    }

    public partial class Meta
    {
        [JsonProperty("current_page")]
        public long CurrentPage { get; set; }

        [JsonProperty("from")]
        public long From { get; set; }

        [JsonProperty("last_page")]
        public long LastPage { get; set; }

        [JsonProperty("path")]
        public string Path { get; set; }

        [JsonProperty("per_page")]
        public long PerPage { get; set; }

        [JsonProperty("to")]
        public long To { get; set; }

        [JsonProperty("total")]
        public long Total { get; set; }
    }
    public partial class Survey
    {
        public static Survey FromJson(string json) => JsonConvert.DeserializeObject<Survey>(json, pg.Factories.Converter.Settings);
    }

    public static class Serialize
    {
        public static string ToJson(this Survey self) => JsonConvert.SerializeObject(self, pg.Factories.Converter.Settings);
    }

    internal static class Converter
    {
        public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
        {
            MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
            DateParseHandling = DateParseHandling.None,
            Converters = {
                new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }
            },
        };
    }
}

Я также определил список:

using System.Collections.Generic;
using System.Data;

namespace pg.Factories
{
    public partial class SurveyCollection
    {
        private List<Survey> surveys;

        public List<Survey> Surveys { get => surveys; set => surveys = value; }

        public override bool Equals(object obj)
        {
            return base.Equals(obj);
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public override string ToString()
        {
            return base.ToString();
        }
    }
}

и, наконец, мой код доступа следующий:

//JSon deserialization
JObject dragnsurveySurveys = JObject.Parse(json);
// get JSON result objects into a list
IList<JToken> resultsData = dragnsurveySurveys["data"].Children().ToList();
// serialize JSON results into .NET objects
IList<Survey> searchResults = new List<Survey>();
foreach (JToken result in resultsData)
{
       // JToken.ToObject is a helper method that uses JsonSerializer internally
       Survey searchResult = result.ToObject<Survey>();
       searchResults.Add(searchResult);
}

Моя JSON строка выглядит так:

 {"data":[{"id":"5dd7c09524810c6cfe20d3eb","created_at":"2019-11-21T00:00:00.000000Z","updated_at":"2020-03-02T10:43:40.376000Z","title":"Auto-diagnostic secret des affaires","theme_id":null,"options":{"display_logo":true,"display_progress_bar":true,"display_title":true,"display_questions_numbering":true,"display_pages_numbering":false,"display_page_back_navigation":true,"validate_button_text":"Valider","footer_text":"R\u00e9alis\u00e9 avec Drag'n Survey","language":"fr","timer":{"hours":0,"minutes":0,"seconds":0},"correction_type":"survey"},"collectors":["5dd7c09624810c6cfe20d3f9"],"pages":["5e5ce35c1e6b0f624e014db2","5e5ce35c1e6b0f624e014db3"],"reports":null},{"id":"5dd7ca31953bc051bf7cf073","created_at":"2019-11-22T11:44:49.000000Z","updated_at":"2020-03-05T07:32:35.169000Z","title":"AUTO-DIAGNOSTIC SECRET DES AFFAIRES","theme_id":"5dd7d1deb6e7297b353cd7d3","options":{"display_logo":true,"display_progress_bar":true,"display_title":true,"display_questions_numbering":true,"display_pages_numbering":false,"display_page_back_navigation":true,"validate_button_text":"Valider","footer_text":"Questionnaire \u00e9labor\u00e9 par www.collabium.com","language":"fr","timer":{"hours":0,"minutes":0,"seconds":0},"correction_type":"survey"},"collectors":["5dd7e16ef9996101b546cd32","5dd7e43ca3818a36d709fb25"],"pages":["5dd7ca31953bc051bf7cf074","5dd7ca31953bc051bf7cf07d"],"reports":null},{"id":"5e5ceb38378f3a193a34d59f","created_at":"2020-03-02T11:17:12.308000Z","updated_at":"2020-07-29T15:13:46.296000Z","title":"Diagnostic des actifs immat\u00e9riels","theme_id":"5e875ba24e4c6110a76dda84","options":{"display_logo":true,"display_progress_bar":true,"display_title":true,"display_questions_numbering":false,"display_pages_numbering":true,"display_page_back_navigation":true,"validate_button_text":"Valider","footer_text":null,"language":"fr","timer":{"hours":0,"minutes":0,"seconds":0},"correction_type":"survey"},"collectors":["5e61065552a9824e4a6833c9","5e69170ab645bb31d723d7f4","5e8f2e1fe60809658a719764"],"pages":["5e5d280724d10036be6b47fe","5e665c68c890b45b7f1fa1d5","5e665604beb72d46986cb56a","5e5ceb38378f3a193a34d5a0","5e661b342a129e30bc725fff","5e60b528e7c7b235d97207e6","5e8352b169ff653c2b67d2a7","5e6764f06bbaa0650a69301c","5f201ea9e51f97599f64fc35","5e60b440481f1c6c377906b7","5e60ce0cd059da698d6745b8","5e60e113ad07822b5d20ef3f","5e60b0ddd082642d3a2a89f3","5e60e611f4f155125314c57a"],"reports":null},{"id":"5e7e088ca17c0d10c630a41f","created_at":"2020-03-27T14:07:08.354000Z","updated_at":"2020-04-03T15:58:37.172000Z","title":"Questionnaire de satisfaction","theme_id":"5e875ba24e4c6110a76dda84","options":{"display_logo":true,"display_progress_bar":true,"display_title":true,"display_questions_numbering":true,"display_pages_numbering":true,"display_page_back_navigation":false,"validate_button_text":"Valider","footer_text":null,"language":"fr","timer":{"hours":0,"minutes":0,"seconds":0},"correction_type":"survey"},"collectors":["5e7e0ccb376f6c71844904f2"],"pages":["5e7e088ca17c0d10c630a420"],"reports":null},{"id":"5e8ae80decc1c1687b377e14","created_at":"2020-04-06T08:27:57.070000Z","updated_at":"2020-04-20T07:30:47.104000Z","title":"Votre capital marque","theme_id":"5e875ba24e4c6110a76dda84","options":{"display_logo":true,"display_progress_bar":false,"display_title":true,"display_questions_numbering":false,"display_pages_numbering":false,"display_page_back_navigation":true,"validate_button_text":"Valider","footer_text":"\u00a9 Diag n' Grow 2020","language":"fr","timer":{"hours":0,"minutes":0,"seconds":0},"correction_type":"survey"},"collectors":["5e8f32aec147ec187a66ad8c","5e8f3a65378ac570f42c2d2b"],"pages":["5e8ae80decc1c1687b377e15","5e8ae80decc1c1687b377e17","5e8ae80decc1c1687b377e39","5e8ae9da0c4da011a4265b86"],"reports":null},{"id":"5e902872f9c33d36896c67d2","created_at":"2020-04-10T08:04:02.728000Z","updated_at":"2020-08-03T07:38:45.252000Z","title":"Diagnostic rapide du Capital Marque","theme_id":"5e875ba24e4c6110a76dda84","options":{"display_logo":true,"display_progress_bar":true,"display_title":true,"display_questions_numbering":false,"display_pages_numbering":false,"display_page_back_navigation":true,"validate_button_text":"Valider","footer_text":"\u00a9 Diag n' Grow 2020","language":"fr","timer":{"hours":0,"minutes":0,"seconds":0},"correction_type":"survey"},"collectors":[],"pages":["5e902872f9c33d36896c67d3","5e902872f9c33d36896c67d5","5e902872f9c33d36896c67db","5e902872f9c33d36896c67e6","5e90691670f04d7d676b1562"],"reports":null},{"id":"5e906c32f746c338ba0ab388","created_at":"2020-04-10T12:53:06.209000Z","updated_at":"2020-08-03T07:37:17.783000Z","title":"test v3","theme_id":"5e875ba24e4c6110a76dda84","options":{"display_logo":true,"display_progress_bar":true,"display_title":true,"display_questions_numbering":false,"display_pages_numbering":false,"display_page_back_navigation":true,"validate_button_text":"Valider","footer_text":"\u00a9 Diag n' Grow 2020","language":"fr","timer":{"hours":0,"minutes":0,"seconds":0},"correction_type":"survey"},"collectors":["5e906c484b1a6b3d3f1a1908"],"pages":["5e906c32f746c338ba0ab389","5e906c32f746c338ba0ab38b","5e906c32f746c338ba0ab38e","5e906c32f746c338ba0ab3a0"],"reports":null},{"id":"5ea81b076b1b4459571b03f1","created_at":"2020-04-28T12:01:11.416000Z","updated_at":"2020-05-06T07:50:28.628000Z","title":"Questionnaire test startup num\u00e9rique","theme_id":"5e875ba24e4c6110a76dda84","options":{"display_logo":true,"display_progress_bar":true,"display_title":true,"display_questions_numbering":false,"display_pages_numbering":true,"display_page_back_navigation":true,"validate_button_text":"Valider","footer_text":null,"language":"fr","timer":{"hours":0,"minutes":0,"seconds":0},"correction_type":"survey"},"collectors":["5ea81b7cc4e478527e578715","5eb26c4457c07d2b89365f56"],"pages":["5ea81b076b1b4459571b03f2","5ea81b076b1b4459571b03f4","5ea81b076b1b4459571b03f7","5ea81b076b1b4459571b03fa","5ea81b076b1b4459571b0408","5ea81b076b1b4459571b0416","5ea81b076b1b4459571b0425","5ea81b076b1b4459571b042c","5ea81b076b1b4459571b0437","5ea81b076b1b4459571b0452","5ea81b086b1b4459571b0462","5ea81b086b1b4459571b046f","5ea81b086b1b4459571b047c"],"reports":null},{"id":"5ec4ee4aeb0756676316fdb6","created_at":"2020-05-20T08:46:02.495000Z","updated_at":"2020-05-20T15:03:30.600000Z","title":"audit des actifs immat\u00e9riels d'un projet d'innovation","theme_id":"5e875ba24e4c6110a76dda84","options":{"display_logo":true,"display_progress_bar":true,"display_title":true,"display_questions_numbering":false,"display_pages_numbering":true,"display_page_back_navigation":true,"validate_button_text":"Valider","footer_text":null,"language":"fr","timer":{"hours":0,"minutes":0,"seconds":0},"correction_type":"survey"},"collectors":["5ec5238e04f42b07a674f66a"],"pages":["5ec4ee4aeb0756676316fdb7","5ec4ee4aeb0756676316fdb9","5ec4ee4aeb0756676316fdbc","5ec4ee4aeb0756676316fdbf","5ec4ee4aeb0756676316fdcd","5ec4ee4aeb0756676316fdf1","5ec4ee4aeb0756676316fdfc","5ec4ee4beb0756676316fe17","5ec4ee4beb0756676316fe27","5ec4ee4aeb0756676316fddb","5ec4ee4beb0756676316fe34"],"reports":null},{"id":"5ed614e06098d51b5e779436","created_at":"2020-06-02T08:59:12.386000Z","updated_at":"2020-06-02T15:54:46.767000Z","title":"Quel est le Capital Relationnel de votre entreprise ?","theme_id":"5e875ba24e4c6110a76dda84","options":{"display_logo":true,"display_progress_bar":false,"display_title":true,"display_questions_numbering":false,"display_pages_numbering":false,"display_page_back_navigation":true,"validate_button_text":"Valider","footer_text":"\u00a9 Diag n' Grow 2020","language":"fr","timer":{"hours":0,"minutes":0,"seconds":0},"correction_type":"survey"},"collectors":["5ed61f5f74d21c64a73633f5"],"pages":["5ed614e06098d51b5e779437","5ed614e06098d51b5e779439","5ed614e06098d51b5e77943c","5ed614e06098d51b5e779447"],"reports":null},{"id":"5ed90c50af6a90249a2204e4","created_at":"2020-06-04T14:59:28.336000Z","updated_at":"2020-08-03T07:38:04.730000Z","title":"CEO , quelle perception avez-vous du capital immat\u00e9riel de votre startup ?","theme_id":"5e875ba24e4c6110a76dda84","options":{"display_logo":true,"display_progress_bar":true,"display_title":true,"display_questions_numbering":false,"display_pages_numbering":true,"display_page_back_navigation":true,"validate_button_text":"Valider","footer_text":null,"language":"fr","timer":{"hours":0,"minutes":0,"seconds":0},"correction_type":"survey"},"collectors":[],"pages":["5ed90c50af6a90249a2204e5","5ed90c50af6a90249a2204ed","5ed90c50af6a90249a2204fb","5ed90c50af6a90249a220509","5ed90c50af6a90249a220518","5ed90c50af6a90249a22051f","5ed90c50af6a90249a22052a","5ed90c51af6a90249a220545","5ed90c51af6a90249a220555","5ed90c51af6a90249a220562","5ed90c51af6a90249a22056f"],"reports":null},{"id":"5f201e31e043411e9a5ab4f4","created_at":"2020-07-28T12:46:41.603000Z","updated_at":"2020-07-29T10:16:15.964000Z","title":"Diag n'Grow - archive","theme_id":"5e875ba24e4c6110a76dda84","options":{"display_logo":true,"display_progress_bar":true,"display_title":true,"display_questions_numbering":false,"display_pages_numbering":true,"display_page_back_navigation":true,"validate_button_text":"Valider","footer_text":null,"language":"fr","timer":{"hours":0,"minutes":0,"seconds":0},"correction_type":"survey"},"collectors":[],"pages":["5f201e31e043411e9a5ab4f5","5f201e31e043411e9a5ab4f7","5f201e31e043411e9a5ab4fa","5f201e31e043411e9a5ab4fd","5f201e31e043411e9a5ab50c","5f201e31e043411e9a5ab51a","5f201e31e043411e9a5ab529","5f201e31e043411e9a5ab530","5f201e32e043411e9a5ab53b","5f201e32e043411e9a5ab556","5f201e32e043411e9a5ab566","5f201e32e043411e9a5ab573","5f201e32e043411e9a5ab580"],"reports":null}],"links":{"first":"https:\/\/developer.dragnsurvey.com\/api\/v2.0.0\/surveys?page=1","last":"https:\/\/developer.dragnsurvey.com\/api\/v2.0.0\/surveys?page=1","prev":null,"next":null},"meta":{"current_page":1,"from":1,"last_page":1,"path":"https:\/\/developer.dragnsurvey.com\/api\/v2.0.0\/surveys","per_page":15,"to":12,"total":12}}

У меня нет проблем с получением строки JSON, но при создании объектов C# я столкнулся со следующей проблемой:

Unhandled exception. Newtonsoft.Json.JsonSerializationException: Error converting value "5dd7c09624810c6cfe20d3f9" to type 'System.String[]'. Path 'data[0].collectors[0]'

Есть ли у вас какие-нибудь идеи?

Большое спасибо!

1 Ответ

1 голос
/ 05 августа 2020

В вашей строке JSON свойство "collectors" представляет собой одномерный массив String:

"collectors":["5dd7c09624810c6cfe20d3f9"]

Но в определении вашего объекта вы объявили его двумерным с помощью [ ] []:

public string[][] Collectors { get; set; }

Ошибка заключается в том, что невозможно преобразовать одну строку "5dd7c09624810c6cfe20d3f9" в ожидаемый массив строк. Либо ваш JSON должен выглядеть примерно как

"collectors":[["5dd7c09624810c6cfe20d3f9"], ["5ee...", "5ff..."]]

(или хотя бы

"collectors":[["5dd7c09624810c6cfe20d3f9"]]

), либо ваше определение свойства должно быть

public string[] Collectors { get; set; }

It кстати, похоже, что у вашего свойства "страницы" будет та же проблема.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...