-2

I'm trying without success to deserialize this JSON in c#:

{
    "settings": {
        "path": "http:\/\/www.igormasin.it\/fileuploads\/tanja_23a6id"
    },
    "files": [{
        "file": "\/IMG_0992-Edit_a.jpg"
    }, {
        "file": "\/IMG_1024-Edit_a.jpg"
    }, {
        "file": "\/IMG_1074-Edit_a.jpg"
    }, {
        "file": "\/Untitled-1.jpg"
    }]
}

my code:

public class JsonTxt
{
    public IList<string> settings { get; set; }
    public IList<string> files { get; set; }
}

downloadstring contains the Json text:

 var deserialized = JsonConvert.DeserializeObject<JsonTxt>(downloadString);        
 Console.WriteLine("*************************************************");
 Console.WriteLine(deserialized.settings[0].ToString());
 Console.WriteLine(deserialized.files.Count);

exception:

Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. 
{"name":"value"}) into type 'System.Collections.Generic.IList`1[System.String]' because the type 
requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized 
type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type 
like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also 
be added to the type to force it to deserialize from a JSON object.
Path 'settings.path', line 1, position 20.'

I am not able to understand the error, and what I am suposed to do.. From what I understand IList is wrong, but what else would be the correct thing to write?

sharkyenergy
  • 3,842
  • 10
  • 46
  • 97
  • 3
    Not a direct answer to your question, but [sites like json2csharp.com](http://json2csharp.com/) can help you with your classes if you're having problems visualising the JSON as objects – James Thorpe Mar 17 '20 at 14:55
  • 2
    The items in your `files` array in the JSON are objects with a property called `file` rather than simply an array of strings, ditto `settings`. – Valuator Mar 17 '20 at 14:56
  • Simply put your type `JsonTxt` and the json text you want to deserialize do not match. Both the property `settings` and the property `files` are very different between what is defined and what you are trying to assign. – Igor Mar 17 '20 at 14:58
  • @JamesThorpe Thank you very much this is amazing.. – sharkyenergy Mar 17 '20 at 15:02
  • Does this answer your question? [How do I deserialize a complex JSON object in C# .NET?](https://stackoverflow.com/questions/16339167/how-do-i-deserialize-a-complex-json-object-in-c-sharp-net) – Pavel Anikhouski Mar 17 '20 at 15:23

2 Answers2

5

Your class structure needs to be something like this:

public class JsonTxt
{
    public Settings Settings { get; set; }
    public IList<File> Files { get; set; }
}

public class Settings
{
    public string Path { get; set; }
}

public class File
{
    public string File { get; set; }
}

Settings is an object, not a collection, and Files is a collection of objects not strings.

Johnathan Barclay
  • 18,599
  • 1
  • 22
  • 35
2

Your class type expect IList settings :

public class JsonTxt
{
    public IList<string> settings { get; set; }
    public IList<string> files { get; set; }
}

but in the json you provide is an object

 "settings": {
        "path": "http:\/\/www.igormasin.it\/fileuploads\/tanja_23a6id"
    },

you should change JsonText attributes type to match with your JSON

1pulsif
  • 471
  • 4
  • 14