クリーンな解決策は、JSON配列内の各項目を表すクラスを作成し、文字列をこのクラスのリストに変換して列挙することです。
public class NotificationItem
{
public int EmailNotificationID { get; set; }
public string EmailNotificationName { get; set; }
public bool IsSet { get; set; }
}
そして、あなたはNotificationItem
オブジェクトのリストにJSON文字列を変換するNewtonsoft.Json.JsonConvert.DeserializeObject
の方法を使用することができます。
@{
var items = Newtonsoft.Json.JsonConvert
.DeserializeObject<List<NotificationItem>>("yourJsonStringHere");
}
@foreach (var item in items)
{
<p>@item.EmailNotificationID</p>
<p>@item.EmailNotificationName </p>
}
私の推測では、オブジェクトを配列などにキャストしなければならないと思います。おそらく、それぞれのアイテムを別のものにキャストする必要があります。 JSON.Netを使ってオブジェクトに変換するのはなぜですか? – Eonasdan
'Model.AccountSettings.EmailNotifications'に' EmailNotification'クラスオブジェクトのリストがありますか?それとも単なるストリングですか?文字列の値はどのように見えますか? – Shyju
はいそれは単なる文字列です – Rodney