Convert Newtonsoft.Json.Linq.JArray to a list of specific object type

前端 未结 6 1458
孤街浪徒
孤街浪徒 2020-11-28 18:43

I have the following variable of type {Newtonsoft.Json.Linq.JArray}.

properties[\"Value\"] {[
  {
    \"Name\": \"Username\",
    \"Selected\":         


        
6条回答
  •  悲&欢浪女
    2020-11-28 19:13

    using Newtonsoft.Json.Linq;
    using System.Linq;
    using System.IO;
    using System.Collections.Generic;
    
    public List GetJsonValues(string filePath, string propertyName)
    {
      List values = new List();
      string read = string.Empty;
      using (StreamReader r = new StreamReader(filePath))
      {
        var json = r.ReadToEnd();
        var jObj = JObject.Parse(json);
        foreach (var j in jObj.Properties())
        {
          if (j.Name.Equals(propertyName))
          {
            var value = jObj[j.Name] as JArray;
            return values = value.ToObject>();
          }
        }
        return values;
      }
    }
    

提交回复
热议问题