1. ホーム

Newtonsoft.Json.Linq.JArray を List<T> に変換する。

2022-03-05 17:17:54
<パス

私は、{Newtonsoft.Json.Linq.JArray} 型の以下の変数を持っています。
次のような配列オブジェクトのセットがあります。

properties["Value"] {[
  {
    "Name": "Username",
    "Selected": true
  },
  {
    "Name": "Password",
    "Selected": true
  }

]}

今度は、次のような型に変換したい。 List<T> ここで、T は SelectableEnumItem です。

public class SelectableEnumItem
    {
        public string Name { get; set; }
        public bool Selected { get; set; }
    }

どうすれば正しく書けるのでしょうか?
これにより、jsonオブジェクトが必要なタイプに変換されます。

 array.ToObject<List<SelectableEnumItem>>() 

ドキュメンテーション
JSONを型に変換する( https://www.newtonsoft.com/json/help/html/ToObjectType.htm )

元記事 https://stackoverflow.com/questions/13565245/convert-newtonsoft-json-linq-jarray-to-a-list-of-specific-object-type