我遇到了某种条件,要求反序列化一个标准但相当大的JSON。
{
"fields": [
{
"webhook": true,
"associated_lookup": null,
"json_type": "jsonobject",
"crypt": null,
"field_label": "Account Owner",
"tooltip": null,
"created_source": "default",
"field_read_only": true,
"section_id": 1,
"read_only": false,
"businesscard_supported": true,
"currency": {},
"id": "3565223000000002421",
"custom_field": false,
"lookup": {},
"visible": true,
"length": 120,
"view_type": {
"view": true,
"edit": true,
"quick_create": false,
"create": true
},
"subform": null,
"api_name": "Owner",
"unique": {},
"data_type": "ownerlookup",
"formula": {},
"decimal_place": null,
"pick_list_values": [],
"multiselectlookup": {},
"auto_number": {}
},
{
"webhook": true,
"associated_lookup": null,
"json_type": "string",
"crypt": null,
"field_label": "Rating",
"tooltip": null,
"created_source": "default",
"field_read_only": false,
"section_id": 1,
"read_only": false,
"businesscard_supported": true,
"currency": {},
"id": "3565223000000002423",
"custom_field": false,
"lookup": {},
"visible": true,
"length": 120,
"view_type": {
"view": true,
"edit": true,
"quick_create": false,
"create": true
},
"subform": null,
"api_name": "Rating",
"unique": {},
"data_type": "picklist",
"formula": {},
"decimal_place": null,
"pick_list_values": [
{
"display_value": "-None-",
"actual_value": "-None-"
},
{
"display_value": "Acquired",
"actual_value": "Acquired"
},
{
"display_value": "Active",
"actual_value": "Active"
},
{
"display_value": "Market Failed",
"actual_value": "Market Failed"
},
{
"display_value": "Project Cancelled",
"actual_value": "Project Cancelled"
},
{
"display_value": "Shut Down",
"actual_value": "ShutDown"
}
],
"multiselectlookup": {},
"auto_number": {}
},{
"webhook": true,
"associated_lookup": null,
"json_type": "string",
"crypt": null,
"field_label": "Account Type",
"tooltip": null,
"created_source": "default",
"field_read_only": false,
"section_id": 1,
"read_only": false,
"businesscard_supported": true,
"currency": {},
"id": "3565223000000002441",
"custom_field": false,
"lookup": {},
"visible": true,
"length": 120,
"view_type": {
"view": true,
"edit": true,
"quick_create": false,
"create": true
},
"subform": null,
"api_name": "Account_Type",
"unique": {},
"data_type": "picklist",
"formula": {},
"decimal_place": null,
"pick_list_values": [
{
"display_value": "-None-",
"actual_value": "-None-"
},
{
"display_value": "Analyst",
"actual_value": "Analyst"
},
{
"display_value": "Competitor",
"actual_value": "Competitor"
},
{
"display_value": "Customer",
"actual_value": "Customer"
},
{
"display_value": "Distributor",
"actual_value": "Distributor"
},
{
"display_value": "Integrator",
"actual_value": "Integrator"
},
{
"display_value": "Investor",
"actual_value": "Investor"
},
{
"display_value": "Other",
"actual_value": "Other"
},
{
"display_value": "Partner",
"actual_value": "Partner"
},
{
"display_value": "Press",
"actual_value": "Press"
},
{
"display_value": "Prospect",
"actual_value": "Prospect"
},
{
"display_value": "Reseller",
"actual_value": "Reseller"
},
{
"display_value": "Supplier",
"actual_value": "Supplier"
},
{
"display_value": "Vendor",
"actual_value": "Vendor"
}
],
"multiselectlookup": {},
"auto_number": {}
}
]}
我已在JSON的一部分上方附加了内容。下面提供了我的反序列化和处理C#代码。我要做的是为数据类型标记为拾取列表的元素(“ data_type”:“ picklist”)的内部标签“ pick_list_values”创建一个链接列表。
JObject ZohoCRMFieldsJO = JObject.Parse(ZohoCRMFieldsJson);
IList<ZohoCRMFields> newZohoCRMField = ZohoCRMFieldsJO["fields"].Select(p => new ZohoCRMFields
{
zohocrmmoduleid = zohocrmmoduleid.ToString(),
webhook = (bool)p["webhook"],
json_type = (string)p["json_type"],
field_label = (string)p["field_label"],
created_source = (string)p["created_source"],
field_read_only = (bool)p["field_read_only"],
section_id = (string)p["section_id"],
read_only = (bool)p["read_only"],
id = (string)p["id"],
custom_field = (string)p["custom_field"],
length = (int)p["length"],
visible = (bool)p["visible"],
api_name = (string)p["api_name"],
data_type = (string)p["data_type"]
}).ToList();
foreach (var ZohoCRMField in newZohoCRMField)
{
if (ZohoCRMField.data_type == "picklist" )
{
var ZohoCRMPickListsProvider = new ZohoCRMPickListsProvider();
IList<ZohoCRMPickLists> newZohoCRMPickList = ZohoCRMFieldsJO["fields"].Select(p => new ZohoCRMPickLists
{
zohocrmpicklistmoduleid = zohocrmmoduleid,
zohocrmfieldid = (string)p["id"],
display_value = (string)p[newZohoCRMField.IndexOf(ZohoCRMField)]["pick_list_values"]["display_value"],
actual_value = (string)p[newZohoCRMField.IndexOf(ZohoCRMField)]["pick_list_values"]["actual_value"]
}).ToList();
}
}
如何完成反序列化。需要获取对应于数据类型为pickuplist的节的pickuplist。任何合适的替代方法也可以使用,但是输出必须在LinkedList <>中。
答案 0 :(得分:2)
您可以创建一个与您的json结构匹配的模型,反序列化该字符串,然后使用linq获取所需的数据,而不是使用JObject.Parse()解析json字符串。
C#模型:
public class FieldList
{
public List<Field> fields { get; set; }
}
public class Field
{
public bool webhook { get; set; }
public object associated_lookup { get; set; }
public string json_type { get; set; }
public object crypt { get; set; }
public string field_label { get; set; }
public object tooltip { get; set; }
public string created_source { get; set; }
public bool field_read_only { get; set; }
public int section_id { get; set; }
public bool read_only { get; set; }
public bool businesscard_supported { get; set; }
public Currency currency { get; set; }
public string id { get; set; }
public bool custom_field { get; set; }
public Lookup lookup { get; set; }
public bool visible { get; set; }
public int length { get; set; }
public ViewType view_type { get; set; }
public object subform { get; set; }
public string api_name { get; set; }
public Unique unique { get; set; }
public string data_type { get; set; }
public Formula formula { get; set; }
public object decimal_place { get; set; }
public List<PickListValues> pick_list_values { get; set; }
public MultiSelectLookup multiselectlookup { get; set; }
public Auto_Number auto_number { get; set; }
}
public class Currency
{
}
public class Lookup
{
}
public class ViewType
{
public bool view { get; set; }
public bool edit { get; set; }
public bool quick_create { get; set; }
public bool create { get; set; }
}
public class Unique
{
}
public class Formula
{
}
public class MultiSelectLookup
{
}
public class Auto_Number
{
}
public class PickListValues
{
public string display_value { get; set; }
public string actual_value { get; set; }
}
反序列化json并获取值:
string json = "....";
var values = new List<PickListValues>();
var fieldList = JsonConvert.DeserializeObject<FieldList>(json) ?? new FieldList();
if (fieldList.fields != null)
{
values = fieldList.fields.Where(x => x.data_type.Equals("picklist"))
.SelectMany(x => x.pick_list_values)
.ToList();
}
var linkedList = new LinkedList<PickListValues>(values);