I am trying to parse the following complex JSON result, which is returned from the Zoho Crm API:
{
\"response\":
{
\"result\":
{
\"Contacts\":
Can I ask you something? Are you the one exporting the JSON? I ask this because the format is quite weird and it does get in the way of your code.
A simpler format would allow you to serialize the string pretty much in a direct way.
Use the below classes for de-serializing using JSON.Net
public class ResponseActual
{
[JsonProperty("response")]
public Response2 Response { get; set; }
}
public class Response2
{
[JsonProperty("result")]
public Result Result { get; set; }
[JsonProperty("uri")]
public string Uri { get; set; }
}
public class Result
{
[JsonProperty("Contacts")]
public Contacts Contacts { get; set; }
}
public class Contacts
{
[JsonProperty("row")]
public IList<Row> Row { get; set; }
}
public class Row
{
[JsonProperty("no")]
public string No { get; set; }
[JsonProperty("FL")]
public IList<FL> FL { get; set; }
}
public class FL
{
[JsonProperty("content")]
public string Content { get; set; }
[JsonProperty("val")]
public string Val { get; set; }
}
//To De-serialize
ResponseActual respone = JsonConvert.DeserializeObject<ResponseActual>(jSON_sTRING)
//Get the contacts list
List<FL> contacts = respone.Response.Result.Contacts.Row[0].FL.ToList();
//Now Get the required value using LINQ
var value = contacts.Where<FL>((s, e) => s.Val =="Email").Select(x=>x.Content).Single();
You may also checkout this - Deserializing JSON to .NET object using Newtonsoft (or LINQ to JSON maybe?)
You need to include the following portion of JSON in your deserialization object Type :
{
"response":
{
"result":
{
"Contacts":
{
"row":
[
{
"no":"1",
"FL":
Class type : 'Contact' is inadequate.
you can use this code:
dynamic dictionary = (JsonConvert.DeserializeObject>(jsonstring))["response"];
var result = dictionary.result;
var contact= result.Contacts;
var row= contact.row;
foreach (var item in row)
{
var no= item.no;
}