如何反序列化 Xamarin Forms 中的多个对象?

How to deserialize multiple objects in Xamarin Forms?

我有两个不同的 class 使用我正在通过 API 向另一个应用程序发送 json 响应,简而言之,我得到以下 json 响应:

{  
   "lmob_Forms":[  
      {  
         "Fields":"Certificate Name",
         "Validation":"R",
         "TabIndex":"1",
         "FieldType":"DropdownList",
         "DateFormate":"",
         "Details":null
      },
   ],
   "drop_Salutation":[  
      {  
         "id":1,
         "idvalue":"Kumari"
      },
      {  
         "id":2,
         "idvalue":"Mrs"
      },
      {  
         "id":3,
         "idvalue":"Ms"
      }
   ]
}

我已经搜索过相同但无法解决这个问题,需要帮助,在此先感谢:)

使用json2csharp

public class LmobForm
{
    public string Fields { get; set; }
    public string Validation { get; set; }
    public string TabIndex { get; set; }
    public string FieldType { get; set; }
    public string DateFormate { get; set; }
    public object Details { get; set; }
}

public class DropSalutation
{
    public int id { get; set; }
    public string idvalue { get; set; }
}

public class RootObject
{
    public List<LmobForm> lmob_Forms { get; set; }
    public List<DropSalutation> drop_Salutation { get; set; }
}

首先为您的 json 对象创建一个模型。

public class LmobForm
{
  public string Fields { get; set; }
  public string Validation { get; set; }
  public string TabIndex { get; set; }
  public string FieldType { get; set; }
  public string DateFormate { get; set; }
  public object Details { get; set; }
}

public class DropSalutation
{
  public int id { get; set; }
  public string idvalue { get; set; }
}

public class RootObject
{
  public List<LmobForm> lmob_Forms { get; set; }
  public List<DropSalutation> drop_Salutation { get; set; }
}

现在,只需使用 Newtonsoft.Json 将您的 JSON 对象反序列化为:

var myObj = JsonConvert.DeserializeObject<RootObject>(jsonString);