简体   繁体   中英

JSon Type is not supported for deserialization of an array in C#

I'm having an issue with some code that I've used previously and I'm sure I'm just being daft but I'm failing to find the reason for this.

The error I'm getting is "Type 'ScriptMain+Imports' is not supported for deserialization of an array."

 using System; using System.Net; using System.Collections.Generic; using System.Web.Script.Serialization; [Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute] public class ScriptMain : UserComponent { string json; public override void PreExecute() { base.PreExecute(); string url = this.Variables.apiurl; ; json = DownloadJson(url); } public override void CreateNewOutputRows() { JavaScriptSerializer serialize = new JavaScriptSerializer(); Imports imports = (Imports)serialize.Deserialize(json, typeof(Imports)); foreach (var contentType in imports.contents) { Output0Buffer.name = contentType.name; } } public static string DownloadJson(string downloadURL) { using (WebClient client = new WebClient()) { return client.DownloadString(downloadURL); } } public class Imports { public List<Contents> contents { get; set; } } public class Contents { public string name { get; set; } } } 

I feel that the only big difference is that on this Json import there is no Root identity, but I thought I'd resolved this using the public class Imports.

The values are coming from a URL, which unfortunately I can't provide due to an API key, but a sample of the results are below, however I'm currently only interested in the Name Value:

 [{"contentType":"SocialWeb","createDate":"2018-04-12 14:07:07","description":"Test Query 1","displayImgUrl":"","editDate":"2018-05-25 13:21:09","fhTwitterStatus":"OFF","fromDate":null,"languageFilters":["English"],"name":"Query1","sharing":"PUBLIC","status":"Active","timeInterval":"LAST_SIX_MONTHS","toDate":null,"topicId":"948015"}, {"contentType":"SocialWeb","createDate":"2017-09-29 10:53:21","description":"Test Query 2","displayImgUrl":"","editDate":"2017-10-18 03:06:41","fhTwitterStatus":"OFF","fromDate":"2017-06-18 02:25:13","languageFilters":["English"],"name":"Query2","sharing":"PUBLIC","status":"Standby","timeInterval":"OPEN_END","toDate":null,"topicId":"845703"}, {"contentType":"SocialWeb","createDate":"2016-11-24 00:53:50","description":"Test Query 3","displayImgUrl":"","editDate":"2016-11-24 00:54:31","fhTwitterStatus":"OFF","fromDate":"2016-11-01 00:00:00","languageFilters":["English"],"name":"Query3","sharing":"PUBLIC","status":"Standby","timeInterval":"OPEN_END","toDate":null,"topicId":"689969"},] 

Any help would be greatly appreciated.

Thanks.

try with Microsoft Newtonsoft.Json like

string json = @"{
  'Name': 'Bad Boys',
  'ReleaseDate': '1995-4-7T00:00:00',
  'Genres': [
    'Action',
    'Comedy'
  ]
}";

then you write simply

Movie m = JsonConvert.DeserializeObject<Movie>(json);

string name = m.Name;

here Movie is class it contains all keys from json

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