简体   繁体   中英

Deserialization using newtonsoft

I'm new to C# and i want to deserialize the JSON file into the API using NewtonSoft but it lead me to this error Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON array (eg [1,2,3]) into type 'API.JSonConfiguration.exampleLibrary' because the type requires a JSON object (eg {"name":"value"}) to deserialize correctly.

"To fix this error either change the JSON to a JSON object (eg {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (eg ICollection, IList) like List 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.'"

        string url = @"http://180.232.67.229/api/jfiller";
        string Data = new WebClient().DownloadString(url
        exampleLibrary json = JsonConvert.DeserializeObject<exampleLibrary>(Data);
        MessageBox.Show(json.filler_id);

exampleLibrary Class:

    public string filler_id { get; set; }
    public string filler_title { get; set; }
    public string filler_type { get; set; }

JSON

[
 {
    "filler_id":"1",
    "filler_title":"Star118 E-CarDemo",
    "filler_type":"1",
    "filler_file":"Star118CarTeaser‌​1.mp4",
    "filler_durat‌​ion":"83",
    "created_a‌​t":"2017-06-10 09:08:41",
    "updated":"2017-06-10 09:08:41","status":"0"
  },
  {
    "filler_id":"2",
    "filler_title":"Sta‌​r118",
    "filler_type":‌​"2",
    "filler_file":"S‌​tar118Solar1.PNG",
    "f‌​iller_duration":"10"‌​,
    "created_at":"2017-‌​06-10 09:09:26",
    "updated":"2017-06-10 09:09:26","status":"0"
  }
]

Try

JsonConvert.DeserializeObject<IEnumerable<exampleLibrary>>(Data);

You have an array of data but don't specify it in your JsonConvert or in your exampleLibrary Class(That I can see from your posted code).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM