プロパティを再帰的に取得したいです。
こんにちは。 いつもたいへんお世話になっております。
Windows10でアプリケーションを開発しています。
Visual Studio 2015 Communityを使っています。
###実現したいこと
プロパティを再帰的に取得したいです。
複雑な構造のJSONをパースした場合、まずとりあえず一覧で見てみたい、と考えました。
かんたんにlogする方法を教えてください。いまは手動で書いていますが、"項目名=" + 項目内容のようなlogを簡単に出力できませんか?
###該当のソースコード
C#
1 public static string ReadJSON(string jsonstring) { 2 string log = ""; 3 try { 4 List<ChatWorkTaskClass> cwtasks = JsonConvert.DeserializeObject<List<ChatWorkTaskClass>>(jsonstring); 5 foreach (ChatWorkTaskClass task in cwtasks) { 6 log += "●" + task.limit_time + task.body + "\r\n"; 7//ここで、"項目名=" + 項目内容のようなlogを作りたい。 8 } 9 } 10 catch (Exception exception) { 11 string error = exception.Message; 12 } 13 return log; 14 } 15 } 16} 17 18public class ChatWorkTasks { 19 public List<ChatWorkTaskClass> ChatWorkProperty { get; set; } 20} 21 22public class ChatWorkTaskClass { 23 public int task_id { get; set; } 24 public Account account { get; set; } 25 public Account assigned_by_account { get; set; } 26 public int message_id { get; set; } 27 public string body { get; set; } 28 public int limit_time { get; set; } 29 public string status { get; set; } 30} 31
C#
1foreach (ChatWorkTaskClass task in cwtasks) { 2 PropertyInfo[] infos = task.GetType().GetProperties(); 3 foreach (PropertyInfo info in infos) 4 log += info.Name + "\t" + info.GetValue(task, null) + "\r\n";
で1階層はできました。自動で階層をたどるようにしたいです。
###補足情報(言語/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
です。
よろしくお願いします。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/10/11 03:22
2016/10/11 03:25
2016/10/11 03:33