8

I'm trying to deserialize an array using Newtonsoft so i can display files from a cloud based server in a listbox but i always end up getting this error no matter what i try:

Newtonsoft.Json.JsonReaderException: 'Unexpected character encountered while parsing value: [. Path '[0].priv', line 4, position 15.'

Thisis an example try to deserialize:

[
 {
  "code": 200,
  "priv": [
     {
        "file": "file.txt",
        "ext": "txt",
        "size": "104.86"
     },
     {
        "file": "file2.exe",
        "ext": "exe",
        "size": "173.74"
     },

  ],
  "pub": [
     {
        "file": "file.txt",
        "ext": "txt",
        "size": "104.86"
     },
     {
        "file": "file2.exe",
        "ext": "exe",
        "size": "173.74"
     }
  ]
 }
]

I tried using a C# Class like this:

    public class ListJson
{
    [JsonProperty("pub")]
    public List List { get; set; }
}

public class List
{
    [JsonProperty("file")]
    public string File { get; set; }

    [JsonProperty("ext")]
    public string Ext { get; set; }

    [JsonProperty("size")]
    public string Size { get; set; }
}
    [JsonProperty("priv")]
public List List { get; set; }
}

public class List
{
    [JsonProperty("file")]
    public string File { get; set; }

    [JsonProperty("ext")]
    public string Ext { get; set; }

    [JsonProperty("size")]
    public string Size { get; set; }
}

And deserialize with:

List<list> fetch = Newtonsoft.Json.JsonConvert.DeserializeObject<List<list>>(json);
7
  • Have you used this to validate your JSON hint it has a syntax error Commented May 16, 2017 at 21:09
  • 3
    you have 2 classes called List...? Commented May 16, 2017 at 21:11
  • 1
    the name List as a class is confusing Commented May 16, 2017 at 21:11
  • 1
    Also, there is a C# class called List - please refrain from confusing the name of classes that already exist. Commented May 16, 2017 at 21:12
  • 1
    If you are using Visual Studio - you can copy the JSON into memory using (ctrl+c) . Click on edit --> Paste Special --> Paste Json as Classes. Commented May 16, 2017 at 21:31

2 Answers 2

13

The correct C# class structure for your JSON is the following:

public class FileEntry
{
    public string file { get; set; }
    public string ext { get; set; }
    public string size { get; set; }
}

public class FileList
{
    public int code { get; set; }
    public List<FileEntry> priv { get; set; }
    public List<FileEntry> pub { get; set; }
}

Deserializing it in this way:

var fetch = JsonConvert.DeserializeObject<FileList[]>(json);
var fileList = fetch.First(); // here we have a single FileList object

As said in the other answer, creating a class called List doesn't automagically turn it into a collection of objects. You need to declare the types to be deserialized from an array a collection type (e.g. List<T>, T[], etc.).

Small tip: when in doubt, use json2csharp.com to generate strongly typed classes from a json string.

Sign up to request clarification or add additional context in comments.

6 Comments

Now i'm stuck on actually using the deserialized json :/ i really suck at this
@Zeq stuck where? Here it is a small demo on .NET Fiddle.
Okay thanks a lot i will try to figure it out from here also the json gets downloaded from a server so it changes depending on the files
@Zeq what part changes?
The json i use this to download the file on start client.DownloadFile("https://www.zeroside.co/api/list/" + fetch[0].id + "/" + fetch[0].token, zerolist);
|
0

At the moment List has a single List instance called priv, which despite the name: doesn't make it a list. To deserialize a JSON array ("priv": [...]), it needs to an array or list-like type, for example List<T> for some T. Presumably a List<FileThing>, if we assume that FileThing is actually the second type called List (you have 2).

2 Comments

My class is called list
@Zeq they're not both called List (at least: not in the same namespace)... because that isn't legal C#... but that isn't the point. The problem isn't the name... it is that it needs to be an array or list-like type. You could rename your List type(s) to Banana, and everything I said would remain identical.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.