C# 用C解析复杂的JSON结果#

C# 用C解析复杂的JSON结果#,c#,.net,json,serialization,zoho,C#,.net,Json,Serialization,Zoho,我试图解析以下复杂的JSON结果,它是从Zoho Crm API返回的: { "response": { "result": { "Contacts": { "row": [ { "no":"1", "FL": [

我试图解析以下复杂的JSON结果,它是从Zoho Crm API返回的:

{
"response":
{
    "result":
    {
        "Contacts":
        {
            "row":
            [
                {
                    "no":"1",
                    "FL":
                    [
                        {
                            "content":"555555000000123456",
                            "val":"CONTACTID"
                        },
                        {
                            "content":"555555000000012345",
                            "val":"SMOWNERID"
                        },
                        {
                            "content":"John Doe",
                            "val":"Contact Owner"
                        },
                        {
                            "content":"Pete",
                            "val":"First Name"
                        },
                        {
                            "content":"Smith",
                            "val":"Last Name"
                        },
                        {
                            "content":"pete@mail.com",
                            "val":"Email"
                        },
                        {
                            "content":"5555551000000012346",
                            "val":"SMCREATORID"
                        },
                        {
                            "content":"Jane Doe",
                            "val":"Created By"
                        },
                        {
                            "content":"555555000000012347",
                            "val":"MODIFIEDBY"
                        },
                        {
                            "content":"Doris Doe",
                            "val":"Modified By"
                        },
                        {
                            "content":"2013-06-14 17:24:10",
                            "val":"Created Time"
                        },
                        {
                            "content":"2013-06-14 17:24:10",
                            "val":"Modified Time"
                        },
                        {
                            "content":"2013-06-14 17:28:05",
                            "val":"Last Activity Time"
                        }
                    ]
                },
                {
                    ...
                }
            ]
        }
    },
    "uri":"/crm/private/json/Contacts/getRecords"
}
}

以下是我的对象的外观:

public class Contact
{
    [JsonProperty(PropertyName = "CONTACTID")]
    public string ContactID { get; set; }
    [JsonProperty(PropertyName = "SMOWNERID")]
    public string OwnerID { get; set; }
    [JsonProperty(PropertyName = "Contact Owner")]
    public string ContactOwner { get; set; }
    [JsonProperty(PropertyName = "First Name")]
    public string FirstName { get; set; }
    [JsonProperty(PropertyName = "Last Name")]
    public string LasName { get; set; }
    [JsonProperty(PropertyName = "Email")]
    public string Email { get; set; }
    [JsonProperty(PropertyName = "SMCREATORID")]
    public string CreatorID { get; set; }
    [JsonProperty(PropertyName = "Created By")]
    public string CreatedBy { get; set; }
    [JsonProperty(PropertyName = "MODIFIEDBY")]
    public string ModifiedByID { get; set; }
    [JsonProperty(PropertyName = "Modified By")]
    public string ModifiedBy { get; set; }
    [JsonProperty(PropertyName = "Created Time")]
    public DateTime CreatedTime { get; set; }
    [JsonProperty(PropertyName = "Modified Time")]
    public DateTime ModifiedTime { get; set; }
    [JsonProperty(PropertyName = "Last Activity Time")]
    public DateTime LastActivityTime { get; set; }
}
“行”模式重复(no 1,2,3…),所以我基本上是想得到这种类型的对象的通用列表。我正在尝试使用JSON.NET,但如果它能让这变得更容易,我愿意接受其他建议

在这种情况下,这显然不起作用:

var response = JsonConvert.DeserializeObject<Contact>(jsonString);
var response=JsonConvert.DeserializeObject(jsonString);
这也不是:

var deserializedObjects = JsonConvert.DeserializeObject<List<Contact>>(jsonString);
var deserializedObjects=JsonConvert.DeserializeObject(jsonString);
这里是我使用JavaScriptSerializer来解析它的一个变通方法,但它是我迄今为止最糟糕的代码块之一

            List<Contact> loContactList = new List<Contact>();
        Contact loContact = null;

        Dictionary<string, object> dictionary = new JavaScriptSerializer().Deserialize<Dictionary<string, object>>(jsonString);
        var response = (Dictionary<string, object>)dictionary["response"];
        var result = (Dictionary<string, object>)response["result"];
        var contacts = (Dictionary<string, object>)result["Contacts"];
        var row = (ArrayList)contacts["row"];

        foreach (var item in row)
        {
            var loArrayItem = (Dictionary<string, object>)item;
            var fl = (ArrayList)loArrayItem["FL"];

            loContact = new Contact();

            foreach (var contactitem in fl)
            {
                var contactdict = (Dictionary<string, object>)contactitem;
                string val = (string)contactdict["val"];
                string content = (string)contactdict["content"];

                if (val == "CONTACTID")
                {
                    loContact.ContactID = content;
                }
                else if (val == "SMOWNERID")
                {
                    loContact.OwnerID = content;
                }
                else if (val == "Contact Owner")
                {
                    loContact.ContactOwner = content;
                }
                else if (val == "First Name")
                {
                    loContact.FirstName = content;
                }
                else if (val == "Last Name")
                {
                    loContact.LastName = content;
                }
                else if (val == "Email")
                {
                    loContact.Email = content;
                }
                else if (val == "SMCREATORID")
                {
                    loContact.CreatorID = content;
                }
                else if (val == "Created By")
                {
                    loContact.CreatedBy = content;
                }
                else if (val == "MODIFIEDBY")
                {
                    loContact.ModifiedByID = content;
                }
                else if (val == "Modified By")
                {
                    loContact.ModifiedBy = content;
                }
                else if (val == "Created Time")
                {
                    loContact.CreatedTime = Convert.ToDateTime(content);
                }
                else if (val == "Modified Time")
                {
                    loContact.ModifiedTime = Convert.ToDateTime(content);
                }
                else if (val == "Last Activity Time")
                {
                    loContact.LastActivityTime = Convert.ToDateTime(content);
                }
            }

            loContactList.Add(loContact);
        }
List loContactList=new List();
联系人loContact=null;
Dictionary Dictionary=new JavaScriptSerializer().Deserialize(jsonString);
变量响应=(字典)字典[“响应”];
变量结果=(字典)响应[“结果”];
var contacts=(字典)结果[“contacts”];
变量行=(ArrayList)联系人[“行”];
foreach(行中的var项目)
{
var loArrayItem=(字典)项;
变量fl=(ArrayList)loArrayItem[“fl”];
loContact=新触点();
foreach(fl中的var contactitem)
{
var contactdict=(字典)contactitem;
字符串val=(字符串)contactdict[“val”];
字符串内容=(字符串)contactdict[“内容”];
如果(val==“联系人ID”)
{
loContact.ContactID=内容;
}
else if(val==“SMOWNERID”)
{
loContact.OwnerID=内容;
}
否则如果(val==“联系所有者”)
{
loContact.ContactOwner=内容;
}
else if(val==“名字”)
{
loContact.FirstName=内容;
}
else if(val==“姓氏”)
{
loContact.LastName=内容;
}
否则,如果(val==“电子邮件”)
{
loContact.Email=内容;
}
否则如果(val==“SMCREATORID”)
{
loContact.CreatorID=内容;
}
else if(val==“创建人”)
{
loContact.CreatedBy=内容;
}
else if(val==“MODIFIEDBY”)
{
loContact.ModifiedByID=内容;
}
else if(val==“修改人”)
{
loContact.ModifiedBy=内容;
}
else if(val==“创建时间”)
{
loContact.CreatedTime=Convert.ToDateTime(内容);
}
else if(val==“修改时间”)
{
loContact.ModifiedTime=Convert.ToDateTime(内容);
}
else if(val==“上次活动时间”)
{
loContact.LastActivityTime=Convert.ToDateTime(内容);
}
}
loContactList.Add(loContact);
}
我已经看过了其他关于StackOverflow的类似文章,但似乎没有一篇能够解决这个问题。有人能解决这个问题吗?我的目标是以一种更优雅的方式解析这个JSON响应,它不涉及一百万个字典对象和ArrayList!任何帮助都将不胜感激

谢谢, 皮特

2013年7月2日更新:

根据Manvik的建议,我提出了以下附加解决方案:

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

List<Contact> loContactList = new List<Contact>();
Contact loContact = null;

ResponseActual respone = JsonConvert.DeserializeObject<ResponseActual>(jsonString);

foreach (var row in respone.Response.Result.Contacts.Row)
{
    loContact = new Contact();

    var rowItem = row.FL.ToList();

    try { loContact.ContactID = rowItem.Where<FL>((s, t) => s.Val == "CONTACTID").Select(x => x.Content).Single(); }
    catch { }
    try { loContact.OwnerID = rowItem.Where<FL>((s, t) => s.Val == "SMOWNERID").Select(x => x.Content).Single(); }
    catch { }
    try { loContact.ContactOwner = rowItem.Where<FL>((s, t) => s.Val == "Contact Owner").Select(x => x.Content).Single(); }
    catch { }
    try { loContact.FirstName = rowItem.Where<FL>((s, t) => s.Val == "First Name").Select(x => x.Content).Single(); }
    catch { }
    try { loContact.LastName = rowItem.Where<FL>((s, t) => s.Val == "Last Name").Select(x => x.Content).Single(); }
    catch { }
    try { loContact.Email = rowItem.Where<FL>((s, t) => s.Val == "Email").Select(x => x.Content).Single(); } catch { }
    try { loContact.CreatorID = rowItem.Where<FL>((s, t) => s.Val == "SMCREATORID").Select(x => x.Content).Single(); }
    catch { }
    try { loContact.CreatedBy = rowItem.Where<FL>((s, t) => s.Val == "Created By").Select(x => x.Content).Single(); }
    catch { }
    try { loContact.ModifiedByID = rowItem.Where<FL>((s, t) => s.Val == "MODIFIEDBY").Select(x => x.Content).Single(); }
    catch { }
    try { loContact.ModifiedBy = rowItem.Where<FL>((s, t) => s.Val == "Modified By").Select(x => x.Content).Single(); }
    catch { }
    try { loContact.CreatedTime = Convert.ToDateTime(rowItem.Where<FL>((s, t) => s.Val == "Created Time").Select(x => x.Content).Single()); }
    catch { }
    try { loContact.ModifiedTime = Convert.ToDateTime(rowItem.Where<FL>((s, t) => s.Val == "Modified Time").Select(x => x.Content).Single()); }
    catch { }
    try { loContact.LastActivityTime = Convert.ToDateTime(rowItem.Where<FL>((s, t) => s.Val == "Last Activity Time").Select(x => x.Content).Single()); }
    catch { }

    loContactList.Add(loContact);
}
公共类响应实际
{
[JsonProperty(“响应”)]
公共响应2响应{get;set;}
}
公共类响应2
{
[JsonProperty(“结果”)]
公共结果结果{get;set;}
[JsonProperty(“uri”)]
公共字符串Uri{get;set;}
}
公开课成绩
{
[JsonProperty(“联系人”)]
公共联系人联系人{get;set;}
}
公共类联系人
{
[JsonProperty(“row”)]
公共IList行{get;set;}
}
公共类行
{
[JsonProperty(“否”)]
公共字符串No{get;set;}
[JsonProperty(“FL”)]
公共IList FL{get;set;}
}
公共课FL
{
[JsonProperty(“内容”)]
公共字符串内容{get;set;}
[JsonProperty(“val”)]
公共字符串Val{get;set;}
}
List loContactList=新列表();
联系人loContact=null;
ResponseActual respone=JsonConvert.DeserializeObject(jsonString);
foreach(respone.Response.Result.Contacts.row中的变量行)
{
loContact=新触点();
var rowItem=row.FL.ToList();
请尝试{loContact.ContactID=rowItem.Where((s,t)=>s.Val==“ContactID”)。选择(x=>x.Content.Single();}
捕获{}
请尝试{loContact.OwnerID=rowItem.Where((s,t)=>s.Val==“SMOWNERID”)。选择(x=>x.Content.Single();}
捕获{}
请尝试{loContact.ContactOwner=rowItem.Where((s,t)=>s.Val==“联系人所有者”)。选择(x=>x.Content.Single();}
捕获{}
请尝试{loContact.FirstName=rowItem.Where((s,t)=>s.Val==“FirstName”)。选择(x=>x.Content.Single();}
捕获{}
请尝试{loContact.LastName=rowItem.Where((s,t)=>s.Val==“LastName”)。选择(x=>x.Content.Single();}
捕获{}
尝试{loContact.Email=rowItem.Where((s,t)=>s.Val==“Email”)。选择(x=>x.Content.Single();}catch{}
请尝试{loContact.CreatorID=rowItem.Where((s,t)=>s.Val==“SMCREATORID”)。选择(x=>x.Content.Single();}
捕获{}
请尝试{loContact.CreatedBy=rowItem.Where((s,t)=>s.Val==“创建人”)。选择(x=>x.Content.Single();}
捕获{}
请尝试{loContact.ModifiedByID=rowItem。
{
"response":
{
    "result":
    {
        "Contacts":
        {
            "row":
            [
                {
                    "no":"1",
                    "FL":
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 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;

            }