こんにちは。
Windows10でアプリケーションを開発しています。
Visual Studio 2015 Communityを使っています。
###前提・実現したいこと
ChatWorkのTaskのJSONをJSONにパースしたいです。
###試したこと
Classを作り、Newtonsoft.JsonをNuGetしました。
###発生している問題・エラーメッセージ
Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'ChatWorkTaskClass' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> 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.
###該当のソースコード
C#
1using System; 2using System.Collections.Generic; 3using System.Linq; 4using System.Text; 5using Newtonsoft.Json; 6 7namespace JSON { 8 partial class JSON { 9 public void ReadJSON(string jsonstring) { 10 string log = ""; 11 try { 12 ChatWorkTaskClass cwtask = JsonConvert.DeserializeObject<ChatWorkTaskClass>(jsonstring); 13 ChatWorkTasks tasks = JsonConvert.DeserializeObject<ChatWorkTasks>(jsonstring); 14 foreach (ChatWorkTaskClass task in tasks.ChatWorkProperty) { 15 log += task.body.ToShiftJis() + "\r\n"; 16 } 17 } 18 catch (Exception exception) { 19 string error = exception.Message; 20 } 21 } 22 } 23} 24 25public class ChatWorkTasks { 26 public ChatWorkTaskClass[] ChatWorkProperty { get; set; } 27} 28 29public class ChatWorkTaskClass { 30 public int task_id { get; set; } 31 public Account account { get; set; } 32 public Assigned_By_Account assigned_by_account { get; set; } 33 public int message_id { get; set; } 34 public string body { get; set; } 35 public int limit_time { get; set; } 36 public string status { get; set; } 37} 38 39public class Account { 40 public int account_id { get; set; } 41 public string name { get; set; } 42 public string avatar_image_url { get; set; } 43} 44 45public class Assigned_By_Account { 46 public int account_id { get; set; } 47 public string name { get; set; } 48 public string avatar_image_url { get; set; } 49} 50
JSON
1[ 2 { 3 "task_id": 0000000, 4 "account": { 5 "account_id": 0000000, 6 "name": "name", 7 "avatar_image_url": "https://tky-chat-work-appdata.s3.amazonaws.com/avatar/ico_default_green.png" 8 }, 9 "assigned_by_account": { 10 "account_id": 0000000, 11 "name": "name", 12 "avatar_image_url": "https://tky-chat-work-appdata.s3.amazonaws.com/avatar/0000/0000.rsz.jpg" 13 }, 14 "message_id": 00000000, 15 "body": "contents", 16 "limit_time": 1477925999, 17 "status": "open" 18 }, 19 { 20 "task_id": 0000000, 21 "account": { 22 "account_id": 0000000, 23 "name": "name", 24 "avatar_image_url": "https://tky-chat-work-appdata.s3.amazonaws.com/avatar/0000/0000.rsz.jpg" 25 }, 26 "assigned_by_account": { 27 "account_id":000000, 28 "name": "name", 29 "avatar_image_url": "https://tky-chat-work-appdata.s3.amazonaws.com/avatar/0000/000000.rsz.jpg" 30 }, 31 "message_id": 00000000, 32 "body": "contents2", 33 "limit_time": 1476457199, 34 "status": "open" 35 } 36]
###補足情報(言語/FW/ツール等のバージョンなど)
Microsoft Visual Studio Community 2015
Version 14.0.25424.00 Update 3
Microsoft .NET Framework
Version 4.6.01038
インストールしているバージョン:Community
Visual C# 2015 00322-20000-00000-AA575
Microsoft Visual C# 2015
です。
よろしくお願いします。
回答1件