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

前端 未结 6 1461
孤街浪徒
孤街浪徒 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:04

    I can think of different method to achieve the same

    IList result= array;
    

    or (i had some situation that this one didn't work well)

    var result = (List) array;
    

    or use linq extension

    var result = array.CastTo>();
    

    or

    var result= array.Select(x=> x).ToArray();
    

    or more explictly

    var result= array.Select(x=> new SelectableEnumItem{FirstName= x.Name, Selected = bool.Parse(x.selected) });
    

    please pay attention in above solution I used dynamic Object

    I can think of some more solutions that are combinations of above solutions. but I think it covers almost all available methods out there.

    Myself I use the first one

提交回复
热议问题