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

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

    The example in the question is a simpler case where the property names matched exactly in json and in code. If the property names do not exactly match, e.g. property in json is "first_name": "Mark" and the property in code is FirstName then use the Select method as follows

    List items = ((JArray)array).Select(x => new SelectableEnumItem
    {
        FirstName = (string)x["first_name"],
        Selected = (bool)x["selected"]
    }).ToList();
    

提交回复
热议问题