簡體   English   中英

C#中的數組反序列化不支持JSon Type

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

我以前使用的一些代碼有問題,我確定自己只是傻子,但我找不到原因。

我收到的錯誤是“數組反序列化不支持Type'ScriptMain + Imports'”。

 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; } } } 

我覺得唯一的區別是在此Json導入上沒有Root身份,但是我認為我已經使用公共類Imports解決了此問題。

這些值來自URL,很遺憾,由於API密鑰,我無法提供這些值,但是下面提供了結果示例,但是我目前僅對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"},] 

任何幫助將不勝感激。

謝謝。

嘗試使用Microsoft Newtonsoft.Json喜歡

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

然后你寫簡單

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

string name = m.Name;

這是Movie類,它包含json中的所有鍵

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM