こんにちは。
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#
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Newtonsoft.Json; namespace JSON { partial class JSON { public void ReadJSON(string jsonstring) { string log = ""; try { ChatWorkTaskClass cwtask = JsonConvert.DeserializeObject<ChatWorkTaskClass>(jsonstring); ChatWorkTasks tasks = JsonConvert.DeserializeObject<ChatWorkTasks>(jsonstring); foreach (ChatWorkTaskClass task in tasks.ChatWorkProperty) { log += task.body.ToShiftJis() + "\r\n"; } } catch (Exception exception) { string error = exception.Message; } } } } public class ChatWorkTasks { public ChatWorkTaskClass[] ChatWorkProperty { get; set; } } public class ChatWorkTaskClass { public int task_id { get; set; } public Account account { get; set; } public Assigned_By_Account assigned_by_account { get; set; } public int message_id { get; set; } public string body { get; set; } public int limit_time { get; set; } public string status { get; set; } } public class Account { public int account_id { get; set; } public string name { get; set; } public string avatar_image_url { get; set; } } public class Assigned_By_Account { public int account_id { get; set; } public string name { get; set; } public string avatar_image_url { get; set; } }
JSON
[ { "task_id": 0000000, "account": { "account_id": 0000000, "name": "name", "avatar_image_url": "https://tky-chat-work-appdata.s3.amazonaws.com/avatar/ico_default_green.png" }, "assigned_by_account": { "account_id": 0000000, "name": "name", "avatar_image_url": "https://tky-chat-work-appdata.s3.amazonaws.com/avatar/0000/0000.rsz.jpg" }, "message_id": 00000000, "body": "contents", "limit_time": 1477925999, "status": "open" }, { "task_id": 0000000, "account": { "account_id": 0000000, "name": "name", "avatar_image_url": "https://tky-chat-work-appdata.s3.amazonaws.com/avatar/0000/0000.rsz.jpg" }, "assigned_by_account": { "account_id":000000, "name": "name", "avatar_image_url": "https://tky-chat-work-appdata.s3.amazonaws.com/avatar/0000/000000.rsz.jpg" }, "message_id": 00000000, "body": "contents2", "limit_time": 1476457199, "status": "open" } ]
###補足情報(言語/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
です。
よろしくお願いします。
まだ回答がついていません
会員登録して回答してみよう