0

I have two different class using that I am sending json response through API to another application, in short I am getting following json response :

{  
   "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"
      }
   ]
}

I have searched regarding the same but not able to solve this problem, Need help, Thanks In Advance :)

Suraj
  • 35
  • 7
  • You can use a tool like https://app.quicktype.io/#l=cs&r=json2csharp to generate the desired class structure and use that when deserializing the JSON. – Nkosi Jul 05 '19 at 10:45
  • Thanks for the response but its giving error, let me share how I am doing deserialize response : var objResponse1 =JsonConvert.DeserializeObject>(content); – Suraj Jul 05 '19 at 11:03
  • Well your JSON had an extra comma in the first array. once you remove that you should be good to go – Nkosi Jul 05 '19 at 11:04
  • Also use just the root object. not `List`, ie `var objResponse1 =JsonConvert.DeserializeObject(content);` – Nkosi Jul 05 '19 at 11:05
  • "Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type System.Collections.Generic.List`1[New_Dynamic_APP.RootObject]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.... – Suraj Jul 05 '19 at 11:07
  • Take a look at the provided answers below. – Nkosi Jul 05 '19 at 11:09
  • oho sorry! by mistake I posted that comma, the original response is correct, it doesn't have extra comma, – Suraj Jul 05 '19 at 11:10

2 Answers2

2

First of all create a model of your json object.

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; }
}

Now, simply use Newtonsoft.Json to deserialize your JSON object as:

var myObj = JsonConvert.DeserializeObject<RootObject>(jsonString);
Sparsha Bhattarai
  • 693
  • 11
  • 20
1

using 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; }
}
Jason
  • 86,222
  • 15
  • 131
  • 146